并发修改异常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 ...
随机推荐
- mysql 5.7版本安装
1.下载tar包,这里使用wget从官网下载 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2. ...
- django验证码captcha
官方文档 https://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation 使用命令安装pip instal ...
- maven配置本地和远程仓库
maven项目中我们不用挨个导入jar包,那这些Jar包从哪里来呢?当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载.首先,Maven 将从本地资源库 ...
- 记录Ubuntu下使用docker使用
关键词:docker.Dockerfile等等. 这里主要记录Ubuntu下docker使用细节. 首先是如何安装,然后如何创建docker镜像.搭建docker服务器.运行使用docker. 1. ...
- 最小化MarkdownPad 2安装体积(win10)
一.原因 MarkdownPad2在Win10当中可能无法正常运行,右侧预览界面会出现错误"This view has crashed!"查阅官网FAQ得知大多数情况下安装Awes ...
- 解决plsql显示问号(???)问题
如何查看Oracle数据库的字符编码 1.查询Oracle Server端的字符集:有很多种方法可以查出oracle server端的字符集,比较直观的查询方法是以下这种:SQL>select ...
- 在Windows系统中安装matplotlib,需要注意的问题
matplotlib的下载地址:https://pypi.org/project/matplotlib/#files 以python3.6为例,适合的版本matplotlib-3.1.1-cp36-c ...
- 利用re处理数据
re模块为Python提供正则表达式功能,我们可以用它来处理数据 大多数时候我们用Python读取文件数据读进来都是字符串,想要提取里面想要的数据用正则表达式是个很不错的手段 下面是一个从读取数据中提 ...
- golang--海量用户即使通讯系统
功能需求: 用户注册 用户登录 显示在线用户列表 群聊 点对点聊天 离线留言
- javascript在数组的循环中删除元素
在开发JavaScript应用的过程中,经常会遇到在循环中移除指定元素的需求. 按照常规的思路,就是对数组进行一个for循环,然后在循环里面进行if判断,在判断中删除掉指定元素即可. 但是实际情况往往 ...