ArrayList中foreach循环中增添、删除导致ConcurrentModificationException
文章目录
一、使用背景
在阿里巴巴开发手册中,有这样一条规定:不要在foreach循环里进行add和remove操作(这里指的是List的add和remove操作),否则会抛出ConcurrentModificationException。remove元素请使用iterator。

二、源码
1.我们知道foreach是语法糖,他本质还是iterator进行的循环,因此下面的代码和使用foreach循环是一样的。在循环里面我们使用“错误”操作,使用List的add方法进行操作,会抛出ConcurrentModificationException
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("apple");
Iterator<String> iterator = arrayList.iterator();
while(iterator.hasNext()){
String value = iterator.next();
if("apple".equals(value)){
arrayList.add("orange");
}
}三、源码解析
1.arrayList.iterator();
①返回Itr类,并将modcount的值赋值给一个变量expectedModCount,其中modcount表示List实际被增删的次数,expectedModCount表示该迭代器期望被增删的次数,当新建Itr类的时候会给他赋初始值,只有通过该迭代器进行值的增删才会修改该值


2.iterator.next();
①在调用迭代器的next方法时,他会进行检查,比较modCount和expectedModCount的值,如果不相等,Concurrent



评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论