0
删除我的列表(整数列表)中的一个元素时出错。 我使用迭代器删除元素从(整数列表)列表中删除元素时发生Java错误
这里是我的代码:
List<List<Integer>> list = new ArrayList<List<Integer>>();
....
....
Iterator<List<Integer>> myListIterator = list.iterator();
int ct1 = 0;
while (myListIterator.hasNext()) {
List<Integer> val = myListIterator.next(); // here is the error
if(ct1 == val.get(0))
list.remove(val);
ct1++;
}
而且我得到这个错误信息:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
有谁知道什么是错我的代码? 谢谢你们!
所以我应该使用for循环吗? – DanielH
它应该工作。 –
是的,它的工作原理,谢谢! – DanielH