并发修改异常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 ...
随机推荐
- 零基础建站如何配置PHP运行环境 几种服务器环境配置的选择和方法
上次给大家分享了小白建站如何选择虚拟空间及服务器,及购买域名的基础知识,这些是硬性要求,你的网站要想运行起来,硬件只是基础,真正的技术是软件,关于PHP软件开发技术,后面我们会慢慢的分享给大家,今天主 ...
- iOS网络开发—POST请求和GET请求
创建GET请求: // 1.设置请求路径 NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJS ...
- 对Android 8.0以上版本通知点击无效的一次分析
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/178 对Android 8.0以上版本通知点击无效的一次分 ...
- [20191106]善用column格式化输出.txt
[20191106]善用column格式化输出.txt # man columnDESCRIPTION The column utility formats its input into mu ...
- Python入门基础学习(时间模块,随机模块)
Python基础学习笔记(六) time模块: 时间的三种表示方法: 1.格式化字符串 2.时间戳 用来表示和1970年的时间间隔,单位为s 3.元组 struct_time 9个元素 time的st ...
- spring mongodb用法
A field annotated with @Id (org.springframework.data.annotation.Id) will be mapped to the '_id' fiel ...
- 201871020225-牟星源 《面向对象程序设计(java)》第一周学习总结
正文 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daiz ...
- [C5W2] Sequence Models - Natural Language Processing and Word Embeddings
第二周 自然语言处理与词嵌入(Natural Language Processing and Word Embeddings) 词汇表征(Word Representation) 上周我们学习了 RN ...
- 《icra16_slam_tutorial_tardos.pdf》
icra16_slam_tutorial_tardos.pdf EKF: https://www.cnblogs.com/gaoxiang12/p/5560360.html 7. 小结 卡尔曼滤波是递 ...
- LG3825/BZOJ4945/LOJ2305 「NOI2017」游戏 dfs+2-SAT
问题描述 LG3825 BZOJ4945 LOJ2305 题解 发现对于每个地图,如果没有\(A,B,C\)地图不可以使用\(a,b,c\),就是一个\(\mathrm{3-SAT}\)问题. 有了这 ...