大杂烩 -- Iterator 并发修改异常ConcurrentModificationException
基础大杂烩 -- 目录
大杂烩 -- Java中Iterator的fast-fail分析
大杂烩 -- Iterator 和 Iterable 区别和联系
问题:
在集合中,判断里面有没有"Hello"这个元素,如果有,我就添加一个"It's me"元素。
使用普通迭代器出现的异常:
ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
产生的原因:
迭代器是依赖于集合而存在的,在判断成功后,集合的中新添加了元素,而迭代器却不知道,所以就报错了,这个错叫并发修改异常。
其实这个问题描述的是:普通迭代器遍历元素的时候,通过集合是不能修改元素的。
解决:
A:迭代器迭代元素,迭代器修改元素
元素是跟在刚才迭代的元素后面的。
B:集合遍历元素,集合修改元素(普通for循环进行遍历,forEach依旧是基于迭代器)
元素是在最后添加的。
package limeMianShi.iterator_; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator; public class It_modify_for_modify { public static void main(String[] args) {
List<String> forStyle = new ArrayList<String>();
List<String> iteratorStyle = new ArrayList<String>(); Iterator<String> iterator = null; forStyle.add("Hello");
forStyle.add("World");
iteratorStyle.add("Hello");
iteratorStyle.add("World");
System.out.print("forStyle---\t");
for (int i = 0; i < forStyle.size(); i++) {
if ("Hello".equals(forStyle.get(i)))
forStyle.add("It's me");
}
iterator = forStyle.iterator();
while (iterator.hasNext())
System.out.print(iterator.next() + " ");
System.out.println(); System.out.print("iteratorStyle---\t");
ListIterator<String> listIterator = iteratorStyle.listIterator();
while (listIterator.hasNext()) {
if ("Hello".equals(listIterator.next()))
listIterator.add("It's me");
}
iterator = iteratorStyle.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
}
}
原理分析:
啦啦啦
大杂烩 -- Iterator 并发修改异常ConcurrentModificationException的更多相关文章
- 理解和解决Java并发修改异常ConcurrentModificationException(转载)
原文地址:https://www.jianshu.com/p/f3f6b12330c1 理解和解决Java并发修改异常ConcurrentModificationException 不知读者在Java ...
- 集合框架之——迭代器并发修改异常ConcurrentModificationException
问题: 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. 使用普通迭代器出现的异常: ...
- 理解和解决Java并发修改异常:ConcurrentModificationException
參考文獻:https://www.jianshu.com/p/f3f6b12330c1 文獻来源:简书 关键字: Java Exception遇到异常信息Exception in thread &qu ...
- 并发修改异常ConcurrentModificationException
1.简述:在使用 迭代器对象遍历集合时,使用集合对象修改集合中的元素导致出现异常 public static void main(String[] args) { List<Integer> ...
- ConcurrentModificationException(并发修改异常)的解决
[异常解释] ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常.[产生的原因] 迭代器是依赖于集合而存在的,在判断成功后,集合 ...
- 并发修改异常(ConcurrentModificationException)
并发修改异常(ConcurrentModificationException) 这个异常,使用集合的时候应该很常见,这个异常产生的原因是因为java中不允许直接修改集合的结构. 先贴上个有趣的例子,给 ...
- ConcurrentModificationException 集合并发修改异常 解决
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; /** * 问题? * 有一个集合, ...
- 详解 迭代器 —— Iterator接口、 ListIterator接口 与 并发修改异常
(请关注 本人"Collection集合"博文--<详解 Collection集合>) Iterator接口(迭代器): 概述: 对 collection 进行迭代的迭 ...
- java 15 - 8 集合框架(并发修改异常的产生原因以及解决方案)
问题? 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. 面试题: Concu ...
随机推荐
- nginx日志分析工具
https://www.linuxidc.com/Linux/2016-12/138731.htm
- Raspberry Pi GPIO Protection
After damaging the GPIO port on our raspberry pi while designing a new solar monitoring system we de ...
- log4j配置输出到多个日志文件
通常我们项目里,有一些重要的日志想单独的输出到指定的文件,而不是全总输出到系统的日志文件中.那么我们log4j为我们提供了这种功能,以下我们来一步一步看是怎么做的.这里以property的配置方式写. ...
- 小程序快速部署富文本插件wxParser
为了解决html2wxml在ios下字体过大问题,又发现一个比较好用的富文本插件:wxParser. 目前 wxParser 支持对一般的富文本内容包括标题.字体大小.对齐和列表等进行解析.同时也支持 ...
- jsp使用c:forEach报错 javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext的问题
今天发现了一个折磨我一天的问题: 在jsp文件中使用 <c:forEach items="${checkResult}" var="item"> & ...
- 动态库DLL中类的使用
一.DLL中类的导出 在类名称前添加 _declspec(dllexport)定义,比如: class _declspec(dllexport) CMath{ .... }; 通常使用预编译开关切换类 ...
- testng.xml 配置大全
1.TestNG的运行方式如下: 1 With a testng.xml file 直接run as test suite 2 With ant 使用ant 3 From the command li ...
- xcode 报错 malloc: *** error for object 0x6c3c5a4: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug------d
大家有时候会遇到这个错误 malloc: *** error for object 0x******: incorrect checksum for freed object - object was ...
- Django Mysql SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
Django 执行makemigrations 的时候报错: django.db.utils.ProgrammingError: (1064, "You have an error in ...
- rocketmq 源码
https://github.com/YunaiV/incubator-rocketmq