循环List删除元素抛ConcurrentModificationException异常原因分析
来源:互联网 发布:sql查询字段包含分号 编辑:程序博客网 时间:2024/05/16 11:42
使用for(Object obj:list)进行循环ArrayList时,如果循环删除list中的元素则会抛出 ConcurrentModificationException异常,如下图:
通过分析错误堆栈我们可以发现使用 for(Object obj:list) 进行循环时,实际上是调用List迭代器(iterator)的next方法进行循环(AbstractList类中的Itr内部类的next方法):
这时,next方法的第一行会对ArryList做校验,如果校验不通过,则抛出ConcurrentModificationException异常
checkForComodification方法:
该方法实际上是对modCount和expectedModCount变量进行比较,如果不同,则抛出异常。
这里涉及到两个变量:
modCount:AbstractList类成员变量。记录List结构变化的次数,每次改变list结构时(add,remove,trimToSize等)都会加1
expectedModCount:Itr(AbstractList类的迭代器)类成员变量,初始时和modCount相等:
了解这两个变量后我们就会发现,for循环中调用的remove方法是ArrayList的remove方法会对modCount变量加1,但expectedModCount变量并未改变。这样,在第二次循环时调用迭代器的next方法就会校验出错。
所以,如果需要对list进行循环删除时要使用迭代器,并调用迭代器的remove方法。
0 0
- 循环List删除元素抛ConcurrentModificationException异常原因分析
- foreach循环删除集合中的元素出现ConcurrentModificationException异常
- foreach循环删除集合中的元素出现ConcurrentModificationException异常(补充)
- 集合遍历时删除元素异常(ConcurrentModificationException)分析
- ConcurrentModificationException异常产生原因分析
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- ArrayList删除元素并发异常ConcurrentModificationException解决方案
- ArrayList循环遍历并删除元素时报java.util.ConcurrentModificationException异常
- JAVA List 一边遍历一边删除元素,报java.util.ConcurrentModificationException异常
- 发生ConcurrentModificationException异常的原因分析
- List删除元素报Exception in thread "main" java.util.ConcurrentModificationException异常,或数据删除不完整
- ConcurrentModificationException异常产生原因
- ConcurrentModificationException异常原因
- Java ConcurrentModificationException异常原因
- Java list集合通过遍历删除元素异常分析
- 更换kernel启动logo图片
- android Intent : android.intent.category.DEFAULT的用途和使用
- 423. Reconstruct Original Digits from English**
- 文章标题
- SQLDMO数据库备份还原 带进度条
- 循环List删除元素抛ConcurrentModificationException异常原因分析
- RSA&AES实现可靠通信
- Android使用DrawerLayout实现抽屉式侧滑菜单
- iOS 硬件授权检测:定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等
- 洛谷 P1032 字串变换
- 清除浮动的几种方法
- tc命令——Linux基于IP进行流量限速
- 收藏本网站代码
- python调用matlab文件