用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的更多相关文章

  1. 集合循环删除问题-报错java.util.ConcurrentModificationException解析

    java.util.ConcurrentModificationException 异常问题详解 环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修 ...

  2. SpringMvc中Hashmap操作遇到 java.util.ConcurrentModificationException: null

    代码按照网上修改为类似,还不能解决问题 for (Iterator<String> it = target.keySet().iterator(); it.hasNext(); ) { i ...

  3. 做Webservice时报错java.util.List是接口, 而 JAXB 无法处理接口。

    Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExc ...

  4. java.util.ConcurrentModificationException: null

    是因为在map.foreach中又put新的值了 在map.foreach中可能是不可以增删改

  5. springboot集成springcloud,启动时报错java.lang.AbstractMethodError: null

    出现这个问题是springboot和springcloud的版本不匹配. 我此处使用了springboot 2.0.4.RELEASE,springcloud 使用了Finchley.SR2. 修改方 ...

  6. java.util.ConcurrentModificationException异常排查

      java.util.ConcurrentModificationException对于这个异常我们一般会认为是在遍历list的时候对这个list做了add,remove等修改操作造成的,最近在线上 ...

  7. list删除操作 java.util.ConcurrentModificationException

    首先大家先看一段代码: public static void main(String[] args) { List<String> listStr = new ArrayList<S ...

  8. 为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作--java.util.ConcurrentModificationException

    摘要 foreach循环(Foreach loop)是计算机编程语言中的一种控制流程语句,通常用来循环遍历数组或集合中的元素. 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体 ...

  9. 遍历List过程中删除操作报java.util.ConcurrentModificationException错误

    1:遍历List 同时 remove 元素,出现java.util.ConcurrentModificationException错误 @Test public void test3(){ List& ...

随机推荐

  1. C--关键字static

    static在C中主要有两个作用: 1.修饰变量  (局部变量.全局变量 都存在内存的静态区) 静态全局变量: 作用域仅限于变量被定义的文件中,其中文件即使用extern声明也无法使用它. 静态局部变 ...

  2. TransactionScope类的使用

    如果在C#中使用TransactionScope类(分布式事务),则须注意如下事项:1.在项目中引用using System.Transactions命名空间(先要在添加net组件的引用); 2.具体 ...

  3. dll 劫持

    库: AheadLib 输入dll 处填你要劫持的dll 路径. 例如: C:\WINDOWS\system32\lpk.dll 来自为知笔记(Wiz)

  4. React知识点总结1

    最近打算把react知识点总结下: React特点 1.虚拟DOM 在内存中操作DOM,在内存中创建数据结构,只会更新有差异的地方 2.组件化 页面分成若干个组件,每个组件包含逻辑结构和样式 组件仅包 ...

  5. Windows转到linux中,文件乱码,文件编码转换 & 解决sqlplus连接oracle乱码

    转载:http://www.cnblogs.com/wanyao/p/3399269.html 最近,学习又重新开始Linux学习,所以一直在Centos中,昨天一朋友把他在Windows下写的C程序 ...

  6. ajax同步异步问题

    之前一直在写JQUERY代码的时候遇到AJAX加载数据都需要考虑代码运行顺序问题.最近的项目用了到AJAX同步.这个同步的意思是当JS代码加载到当前AJAX的时候会把页面里所有的代码停止加载,页面出去 ...

  7. eclipse中jsp文档无语法着色,安装Eclipse Java Web Developer Tools插件

    一.安装Eclipse Java Web Developer Tools插件 1.eclipse菜单:help/install New Software,打开Available Software窗体: ...

  8. mysql使用小技巧

    1.mySql 删除表中大批量的数据 假设有一个表(logs)有2000万条记录,我们要在业 务不停止的情况下删除其中status=1的所有记录,差不多有1800万条,直接执行 DELETE FROM ...

  9. C#中string在内存中是如何表示的

    不知道你是否有过和我一样的疑问,不同编码的字符串是如何存储在运行时的内存中的呢,计算机在操作string类型的对象时,如何知道这个string是什么编码呢?和文本文件那样有类似BOM的东东在strin ...

  10. c# 实现 java 的 System.currentTimeMillis() 值

    本文地址:http://www.cnblogs.com/jying/p/3875331.html 以下一句即可实现 java 中的 System.currentTimeMillis() 值 , , , ...