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 ...
随机推荐
- 前端开发 Vue -3axios
Axios是什么? 应该念“阿克希奥斯”……但是太长太拗口,我一般念“阿笑斯”…… Axios 是一个基于 promise 的 HTTP 库,简单的讲就是可以发送get.post请求.说到get.po ...
- 验证 vector = 是深拷贝还是浅拷贝
#include <vector> using namespace std; int main() { int w=1920; int h = 1080; vector<int> ...
- Vivado问题集锦
1.添加包含子IP的模块到block design,报错如下所示: 错误的后面提供了解决方法:在tcl命令行中输入如下指令,添加子IP的xci文件即可. set_property generate_s ...
- Timestamp,Date和String的互相转换
1.Timestamp,Date和String的互相转换 //Timestamp转换成String: Timestamp ts = new Timestamp(System.currentTimeMi ...
- List、Set、Map集合的遍历方法
一.List集合遍历 public class TraversingList { /** * @author zhuxun describe: 定一个List集合并遍历 */ /** 定义一个List ...
- Android 启动流程分析
原文:https://www.jianshu.com/p/a5532ecc8377 作者曾经在高通的Android性能组工作,主要工作是优化Android Application的启动时间. APP基 ...
- c# 引用类型和值类型的内存分配
- golang使用ssh远程连接服务器并执行命令
安装golang.org/x 直接去github上面,把https://github.com/zieckey/golang.org,把整个目录拷贝下来放到你的gopath下面即可.记住在gopath的 ...
- Linux网络管理——路由配置文件、DNS配置文件、hosts文件
路由配置文件 route命令添加的路由表,重启网络服务或者重启系统之后就全都失效了.可以创建针对网卡的路由配置文件,这样在重启网络服务 或者 重启系统的时候就会加载针对某个网卡的配置文件. CONFI ...
- 什么是微服务 Martin Fowler的microservices
https://martinfowler.com/articles/microservices.html https://martinfowler.com/microservices/ 微服务,最早由 ...