java.util.ConcurrentModificationException)
代码展示
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
for (String s1 : arrayList) {
if ("2".equals(s1)) arrayList.remove("2");
}
arrayList.forEach(s -> {
if ("2".equals(s)) arrayList.remove("2");
});
System.out.println(arrayList);
}
异常信息
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.itpm.project.controller.AcceptApplyController.main(AcceptApplyController.java:41)
异常原因
图片描述
expectedModCount的值是循环刚开始时modCount的值所赋予的
使用ArrayList提供的remove方法,在判断完值不为null后会调用fastRemove方法
在fastRemove方法中会首先将modCount进行自增,此时modCount的值就与expectedModCount的值不同了
加强for或foreach会在最后调用next方法,next方法首先调用了checkForComodification方法
checkForComodification方法中对modCount与expectedModCount进行对比,结果为true,从而抛出了以上异常
文字描述
加强for和foreach是通过迭代器Iterator来访问的,ArrayList的remove方法会调用fastRemove方法,在fastRemove方法中modCount会进行自增,每次加强for和foreach都会调用checkForComodification方法,checkForComodification方法会将modCount与expectedModCount进行比对,而modCount在ArrayList的remove方法在被调用时进行了自增,自然就与expectedModCount不一致,从而抛出了以上异常
解决方法
图片描述
在removeIf方法和remove方法,会重置expectedModCount,以保证多个迭代器同时遍历时的数据一致
代码展示
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.removeIf("2"::equals);
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
if ("2".equals(iterator.next())) iterator.remove();
}
System.out.println(arrayList);
}