可能重複:
Efficient equivalent for removing elements while iterating the CollectionLinkedList的迭代器刪除
private LinkedList flights;
....
public void clear(){
ListIterator itr = flights.listIterator();
while(itr.hasNext()){
flights.remove(itr.next());
}
}
....
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at section1.FlightQueue.clear(FlightQueue.java:44)
at section1.FlightTest001.main(FlightTest001.java:22)
它有什麼不對?完全不知道爲什麼給出錯誤,我確信我已經在arraylists或數組上使用了相同的代碼,並且它已經工作了。
爲什麼不要調用'flights.clear()'? – assylias