Exception in thread "main" java.util.ConcurrentModificationException解决方案
我想判断一个集合里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,
当时的做法是:
public class ListIteratorDemo1 {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// 迭代器遍历
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
if ("world".equals(s)) {
list.add("javaee");
}
}
System.out.println("list:" + list);
}
}
但是报了错误Exception in thread "main" java.util.ConcurrentModificationException错误

查阅API知道:
ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
产生此种的原因是:
迭代器是依赖于集合而存在的,在判断成功后,集合的中新添加了元素,而迭代器却不知道,所以就报错了,这个错叫并发修改异常。
其实这个问题简单的描述是:迭代器遍历元素的时候,通过集合是不能修改元素的。
如何解决呢?
A:迭代器迭代元素,迭代器修改元素
元素是跟在刚才迭代的元素后面的。
B:集合遍历元素,集合修改元素(普通for)
元素在最后添加的。
A的解决方案:
public class ListIteratorDemo1 {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// 方式1:迭代器迭代元素,迭代器修改元素
// 而Iterator迭代器却没有添加功能,所以我们使用其子接口ListIterator
ListIterator lit = list.listIterator();
while (lit.hasNext()) {
String s = (String) lit.next();
if ("world".equals(s)) {
lit.add("javaee");
}
}
System.out.println("list:" + list);
}
}
执行结果:

从结果看出:元素是跟在刚才迭代的元素后面的。
B的解决方案:
public class ListIteratorDemo1 {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// 方式2:集合遍历元素,集合修改元素(普通for)
for (int x = 0; x < list.size(); x++) {
String s = (String) list.get(x);
if ("world".equals(s)) {
list.add("javaee");
}
}
System.out.println("list:" + list);
}
}
执行结果:

从结果看出:元素在最后添加的。
Exception in thread "main" java.util.ConcurrentModificationException解决方案的更多相关文章
- Exception in thread "main" java.util.ConcurrentModificationException
package test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public c ...
- Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character clas ...
- Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 解决方法: 要对切割字符进行转义\\
使用str.split("[",15)时,出现Exception in thread "main" java.util.regex.PatternSyntaxE ...
- Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for topic_test_1219-2: 30010 ms has passed since batch creatio
代码如下 public static void producer1() throws ExecutionException, InterruptedException { Properties pro ...
- Exception in thread "main" java.util.InputMismatchException
今天写代码来了一个异常 /** * 需求分析:根据输入的天数是否是周六或是周日, * 并且天气的温度大于28摄氏度,则外出游泳,否则钓鱼 * @author chenyanlong * 日期:2017 ...
- unit测试出现异常:Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util
在进行单元测试时,测试出现异常 Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform ...
- 解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)
当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOExce ...
- WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable Exception in thread "main" java.io.IOException: No FileSystem for sc F
1.执行脚本程序报如下所示的错误: [hadoop@slaver1 script_hadoop]$ hadoop jar web_click_mr_hive.jar com.bie.hive.mr.C ...
- Exception in thread "main" java.lang.StackOverflowError at java.util.ArrayList$SubList.rangeCheckForAdd(Unknown Source)
Exception in thread "main" java.lang.StackOverflowError at java.util.ArrayList$SubList.ran ...
随机推荐
- logback日志详细解析
1.为什么使用logback 内核重写.测试充分.初始化内存加载更小,这一切让logback性能和log4j相比有诸多倍的提升 logback非常自然地直接实现了slf4j,方便理解 支持自动去除旧的 ...
- MGB的生成代码解析
目录 @ 问题描述 文字描述 问题是在我刚刚学习MyBatis逆向工程时出现的,我发现使用Example是可以创建两个Criteria对象,并且两个对象也都可以添加条件,但是在运行过程中只会执行第一次 ...
- nohub
nohup command > myout.file 2>&1 & nohup command > /dev/null 2>&1 &
- X宝个人支付到账
扫码登录,能看懂的我就不多说了,封了我多少篇文章了!!!!X宝个人到账通知.
- Aop动态代理和cglib
一般我们使用Aop对象时,常用动态代理模式,即是采用映射一个相同的类在此基础上进行前置后置操作. 动态代理多是采用原类实现父类接口,然后动态代理一个和原类相同的双胞胎兄弟类来实现映射. 父类 publ ...
- 数据库—Innodb中的MVVC
文章:Innodb中的MVVC 地址:https://www.jianshu.com/p/7e967d291c24
- gcc生成so文件
准备三个文件test.h, test.c, main.c test.h #include <stdio.h> void say_hello(); test.c #include " ...
- sql/pl 安装并连接Oracle数据库
1,首先,先下载pl/sql devloper 安装包.下载对应版本的安装包 下载地址 https://www.allroundautomations.com/bodyplsqldevreg.htm ...
- Excel 教程一
俗话说,工欲善其事,必先利其器,那么我们今天就先来看一下这个excel软件的一些主要功能菜单. 一.功能区菜单 功能区菜单主要包括: 文件菜单: 主要用于新建文件,保存文件,另存为文件,打开文件,打 ...
- test11111111
test 博文内容中字符过多,拒绝显示 123123123