for(int i=0;i<list.size();i++){
if(list.get(i).equals("A"))
list.remove(i);
}
使用for循環會導致循環不完,因爲下標改變了
使用stream的forEach也不行,會報錯,
正確方法如下:
Iterator<String> it = list.iterator();
while(it.hasNext()){
String x = it.next();
if(x.equals("A")){
it.remove();
}
}
使用迭代器的remove方法