并发修改异常ConcurrentModificationException
1.简述:在使用 迭代器对象遍历集合时,使用集合对象修改集合中的元素导致出现异常
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
list.add(1);list.add(2);list.add(3);list.add(4);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
Integer integer = (Integer) iterator.next();
list.remove(2);
}
}

2.原因:迭代器是依赖于集合的,而集合的改变迭代器并不知道。
3.解决方案:
方式一:迭代器遍历,迭代器修改(ListIterator)
remove:
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
list.add(1);list.add(2);list.add(3);list.add(4);
ListIterator<Integer> iterator = list.listIterator();
while (iterator.hasNext()) {
Integer integer = (Integer) iterator.next();
if(integer==2) {
iterator.remove();
}
}
System.out.println(list);
}

add:
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
list.add(1);list.add(2);list.add(3);list.add(4);
ListIterator<Integer> iterator = list.listIterator();
while (iterator.hasNext()) {
Integer integer = (Integer) iterator.next();
if(integer==1){
iterator.add(1);
}
}
System.out.println(list);
}

方式二:直接使用for循环遍历集合,修改(size()和get())
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
list.add(1);list.add(2);list.add(3);list.add(4);
for (int i = 0; i < list.size(); i++) {
list.set(0, 3);//
if (list.get(i)==2) {
list.remove(i);//
}
if (list.get(i)==3) {
list.add(5);//
}
}
System.out.println(list);
}
并发修改异常ConcurrentModificationException的更多相关文章
- 理解和解决Java并发修改异常ConcurrentModificationException(转载)
原文地址:https://www.jianshu.com/p/f3f6b12330c1 理解和解决Java并发修改异常ConcurrentModificationException 不知读者在Java ...
- 集合框架之——迭代器并发修改异常ConcurrentModificationException
问题: 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. 使用普通迭代器出现的异常: ...
- 大杂烩 -- Iterator 并发修改异常ConcurrentModificationException
基础大杂烩 -- 目录 大杂烩 -- Java中Iterator的fast-fail分析 大杂烩 -- Iterator 和 Iterable 区别和联系 问题: 在集合中,判断里面有没有" ...
- 理解和解决Java并发修改异常:ConcurrentModificationException
參考文獻:https://www.jianshu.com/p/f3f6b12330c1 文獻来源:简书 关键字: Java Exception遇到异常信息Exception in thread &qu ...
- ConcurrentModificationException(并发修改异常)的解决
[异常解释] ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常.[产生的原因] 迭代器是依赖于集合而存在的,在判断成功后,集合 ...
- 并发修改异常(ConcurrentModificationException)
并发修改异常(ConcurrentModificationException) 这个异常,使用集合的时候应该很常见,这个异常产生的原因是因为java中不允许直接修改集合的结构. 先贴上个有趣的例子,给 ...
- ConcurrentModificationException 集合并发修改异常 解决
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; /** * 问题? * 有一个集合, ...
- java 15 - 8 集合框架(并发修改异常的产生原因以及解决方案)
问题? 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. 面试题: Concu ...
- Java基础知识强化之集合框架笔记19:List集合迭代器使用之 并发修改异常的产生原因 以及 解决方案
1. 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. ConcurrentModi ...
随机推荐
- JS 逻辑
JS 逻辑 Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true 或者 false). Boolean 对象 您可以将 Boolean 对象理解为一个产生逻辑值的对象包装器. Boolean ...
- SQL,case ziduan when ziduan_value then 'result'
case a.sex when 0 then '女' when 1 then '男' else '其他' end as sex 当a表的性别字段的value为0时将查询的value转换成 '女',当字 ...
- 使用docker简单搭建个人博客
首先介绍需要的yml文件,docker-compose.yml: version: '3.3' services: db: image: mysql:5.7 volumes: - db_data:/v ...
- 201871010116-祁英红《面向对象程序设计(java)》第十四周学习总结
博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://home.cnblogs.com/u/nwnu-daizh/ 这个作业的要求在哪里 https:/ ...
- (转)玩转Koa -- koa-bodyparser原理解析
地址:http://www.imooc.com/article/274059 一.前置知识 在理解koa-bodyparser原理之前,首先需要了解部分HTTP相关的知识. 1.报文主体 HT ...
- xen原理
目录:1. Xen的简介1.1 Xen的大体结构1.2 Xen对VM的称呼1.3 Xen对CPU和内存的虚拟化过程1.4 Xen对IO设备的虚拟化过程1.5 Linux Kernel对Xen的支持1. ...
- mysql中group by 使用
问题描述 我现在需要查询表test,里面需要安装字段a 进行分组.分组之后还有按照b字段最大的.还要查询出字段c. 我先在使用的数据库是mysql8.0 解决 需注意: group by 分组的时候是 ...
- CF414D Mashmokh and Water Tanks
CF414D Mashmokh and Water Tanks 洛谷评测传送门 题目描述 Mashmokh is playing a new game. In the beginning he has ...
- angular ng-bind-html异常Attempting to use an unsafe value in a safe context处理
在angular中使用ng-data-html渲染dom时,遇到了一个Attempting to use an unsafe value in a safe context错误,官方给出的理由是‘试图 ...
- Saiku嵌入页面plugin=true效果自定义实现(二十七)
Saiku嵌入页面使用 自定义实现 Plugin=true 效果 saiku嵌入页面plugin=true时数据不显示(plugin=false或者不设定plugin的值时数据显示正常)这个问题困扰了 ...