for(String x:list){
if(x.equals("del"))
list.remove(x);
}
會報錯:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
解決辦法:
public class TestIterator {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
Iterator<String> stringIterator=list.iterator();
while(stringIterator.hasNext()){
String s=stringIterator.next();
if(s.equals("c")){
stringIterator.remove();
}
}
}
}