3 回答

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

TA貢獻(xiàn)1815條經(jīng)驗 獲得超6個贊
Collection
Clone
Constructor
.
此異??赡苡蓹z測到對象的并發(fā)修改的方法引發(fā),而這種修改是不允許的。
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貢獻(xiàn)1825條經(jīng)驗 獲得超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); }}
添加回答
舉報