对ArrayList操作时报错java.util.ConcurrentModificationException null
用iterator遍历集合时要注意的地方:不可以对iterator相关的地方做添加或删除操作。否则会报java.util.ConcurrentModificationException
例如如下代码:
ArrayList<String> test = new ArrayList<String>();
test.add("1");
test.add("11");
test.add("111");
test.add("1111");
test.add("11111");
int total = test.size();
for(String temp:test){
test.remove(temp);
}
运行报错如下:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at com.jq.busi.work.alarm.AlarmPushBusi.main(AlarmPushBusi.java:369)
原因分析:
参考一个博客的分析:http://blog.sina.com.cn/s/blog_5a15b7d10101brtj.html
个人的一个修改方法:
ArrayList<String> test = new ArrayList<String>();
test.add("1");
test.add("11");
test.add("111");
test.add("1111");
test.add("11111");
int total = test.size();
for (int count = 0; count < total; count++) {
String temp = test.get(count);
test.remove(temp);
total = test.size();
count--;
}
对ArrayList操作时报错java.util.ConcurrentModificationException null的更多相关文章
- 集合循环删除问题-报错java.util.ConcurrentModificationException解析
java.util.ConcurrentModificationException 异常问题详解 环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修 ...
- SpringMvc中Hashmap操作遇到 java.util.ConcurrentModificationException: null
代码按照网上修改为类似,还不能解决问题 for (Iterator<String> it = target.keySet().iterator(); it.hasNext(); ) { i ...
- 做Webservice时报错java.util.List是接口, 而 JAXB 无法处理接口。
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExc ...
- java.util.ConcurrentModificationException: null
是因为在map.foreach中又put新的值了 在map.foreach中可能是不可以增删改
- springboot集成springcloud,启动时报错java.lang.AbstractMethodError: null
出现这个问题是springboot和springcloud的版本不匹配. 我此处使用了springboot 2.0.4.RELEASE,springcloud 使用了Finchley.SR2. 修改方 ...
- java.util.ConcurrentModificationException异常排查
java.util.ConcurrentModificationException对于这个异常我们一般会认为是在遍历list的时候对这个list做了add,remove等修改操作造成的,最近在线上 ...
- list删除操作 java.util.ConcurrentModificationException
首先大家先看一段代码: public static void main(String[] args) { List<String> listStr = new ArrayList<S ...
- 为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作--java.util.ConcurrentModificationException
摘要 foreach循环(Foreach loop)是计算机编程语言中的一种控制流程语句,通常用来循环遍历数组或集合中的元素. 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体 ...
- 遍历List过程中删除操作报java.util.ConcurrentModificationException错误
1:遍历List 同时 remove 元素,出现java.util.ConcurrentModificationException错误 @Test public void test3(){ List& ...
随机推荐
- ListView 中的ImageView Button
1.ListView 中的ImageView Button的点击事件会屏蔽掉ItemView的点击事件 原因是ImageView Button在回执过程中会抢夺item的焦点 解决办法:在xml中添 ...
- ARCGIS多种影像裁剪
在互联网上下载的遥感影像都进行过分幅处理,下载下来的影像多是规则的四方形,而在进行遥感影像研究时,多是针对特定区域来进行,比如研究北京市的遥感影像,不在北京市范围内的影像对于研究者就没有利用意义,如果 ...
- Docker学习过程中遇到的问题及解决方法
1.重新安装Docker后,运行不起来 [root@zyt-test-14-53 ~]# docker infoCannot connect to the Docker daemon. Is the ...
- js判断页面是pc打开还是手机打开
<script type="text/javascript"> function browserRedirect() { var sUserAgent = naviga ...
- ActiveX: 如何用.inf和.ocx文件生成cab文件
ActiveX: 如何用.inf和.ocx文件生成cab文件
- 机器学习PR:k近邻法分类
k近邻法是一种基本分类与回归方法.本章只讨论k近邻分类,回归方法将在随后专题中进行. 它可以进行多类分类,分类时根据在样本集合中其k个最近邻点的类别,通过多数表决等方式进行预测,因此不具有显式的学习过 ...
- javascript对象引用与赋值
avascript对象引用与赋值 <script type="text/javascript"> //例子一: 引用 var myArrayRef = new Arra ...
- 使用web图标
http://www.lovelucy.info/demo/twitter-bootstrap-custom-icons/
- 策划了个.NET控件的案例大赛
任何一个产品的普及,都有一个过程: 1. 对新事物充满热情.喜欢尝鲜.或后急迫需要的人首先成为产品用户.他们总数很少,但是是产品用户的第一批种子. 2. 思想比较开放.能接受新事物的人会成为第二批用户 ...
- iOS"Request failed: unacceptable content-type: text/html"
接口访问出错了,用浏览器测试,发现可以正常返回数据. 下面是错误信息: 获取服务器响应出错 error=Error Domain=com.alamofire.error.serialization.r ...