JAVA日常出错笔记
泛型容器使用迭代器遍历
问题代码
public static void main(String[] args) {
ArrayList<Exercise> l = new ArrayList<Exercise>();
Iterator<Exercise> it1 = l.iterator();
for (int i = 0; i < 5; i++) {
l.add(new Exercise("hero " + i));
}
//错误发生在while循环中
while (it1.hasNext()) {
Exercise t = it1.next();
}
}
异常内容
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996)
at exercise.Exercise.main(Exercise.java:23)
问题查询
- 使用搜索引擎查询到此异常为迭代器遍历时,修改容器内容,系统不允许发生这种操作,所以抛出异常;
- 底层原因是,使用迭代器遍历时,如果修改集合,集合的 modCount 参数就会发生改变,然后 next() 调用 checkForComodification 这个方法之后抛出异常,如图, 其中 modCount 代表集合被修改的次数,expectedModCount代表集合被迭代器修改的次数;
- 从代码来看,我虽然一开始就声明迭代器,但是并没有使用迭代器循环,而是使用 for 循环遍历添加元素,猜想添加元素后迭代器发生改变, 于是我决定再定义一次迭代器;
public static void main(String[] args) {
ArrayList<Exercise> l = new ArrayList<Exercise>();
Iterator<Exercise> it1 = l.iterator();
for (int i = 0; i < 5; i++) {
l.add(new Exercise("hero " + i));
}
it1 = l.iterator(); //再次定义 发生改变
while (it1.hasNext()) {
Exercise t = it1.next();
}
}
- 经过修改的调试发现,果然这家伙在添加元素后发生了改变,具体改变如下图:
添加元素前:
添加元素后:
原因分析
经过我睿智的分析,发现尽管我在 for循环中添加元素不是使用迭代器添加,但是添加元素后,集合的 modCount 参数发生了改变,而代表迭代器的 expectedModCount 却没发生改变,所以使用 it1.next() 时调用的 checkForComodification 就判断 modCount 与 expectedModCount 两者不相等,最终抛出异常,不让我遍历集合。(岂可休!)
最最最重要的总结部分
使用迭代器之后不要修改集合!!!
尽管你不是使用迭代器修改的~