3 回答

TA貢獻1833條經驗 獲得超4個贊
這個類的迭代器和listIterator方法返回的迭代器是快速失敗的:如果列表在創建迭代器之后的任何時候進行了結構上的修改,那么除了通過迭代器自己的Remove或Add方法,迭代器將拋出一個ConcurrentModificationException。
next()
next()
hasNext()
hasNext()
next()
public boolean hasNext() { return cursor != size();}
next()
摘要
List.remove()
ConcurrentModificationException

TA貢獻1815條經驗 獲得超6個贊
Collection
Clone
Constructor
.
此異常可能由檢測到對象的并發修改的方法引發,而這種修改是不允許的。
final
private static final List<Integer> integerList;
List<Integer> copy = new ArrayList<Integer>(integerList);for(Integer integer : integerList) { if(integer.equals(remove)) { copy.remove(integer); }}

TA貢獻1825條經驗 獲得超4個贊
List<Integer> integerList;integerList = new ArrayList<Integer>();integerList.add(1);integerList.add(2);integerList.add(3);int size= integerList.size();//Item to removeInteger remove = Integer.valueOf(3);
解決辦法:
//To remove items from the list, start from the end and go backwards through the arrayList//This way if we remove one from the beginning as we go through, then we will avoid getting a runtime error//for java.lang.IndexOutOfBoundsException or java.util.ConcurrentModificationException as when we used the iteratorfor (int i=size-1; i> -1; i--) { if (integerList.get(i).equals(remove) ) { integerList.remove(i); }}
添加回答
舉報