用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. 20个编写现代CSS代码的建议

    明白何谓Margin Collapse 不同于其他很多属性,盒模型中垂直方向上的Margin会在相遇时发生崩塌,也就是说当某个元素的底部Margin与另一个元素的顶部Margin相邻时,只有二者中的较 ...

  2. 艺术品照片融合到背景墙上效果及DEMO

    演示地址: 功能:图片按照角度增加阴影,比较复杂的功能就是当墙面是不规则的时候,贴的艺术品必须按照墙面的角度进行变形处理.

  3. Java并发编程:Callable、Future和FutureTask

    作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本博客中未标明转载的文章归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...

  4. Asp.net 未处理异常

    页面级捕获未处理异常 - Page 的 Error 事件 Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.Even ...

  5. Ixia测试仪的自动化

    Ixia,美国Ixia公司的通信网络测试仪. 1.Ixia的自动化测试场景 测试PC(安装IxOS)AT框架-->Tcl驱动库SIG_Teq_Ixia.tcl(加载 IxTclHal库)---- ...

  6. Java日志——2016.6.3

    1)二维数组的静态初始化:                int[][] arr = new int[][] { {1,2,3}, {4,5}, {6,7}} 2)杨辉三角: /** *    需求: ...

  7. 填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT

    填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT 警告:发生了重入! 解释:在主循环里调用了一个函数,而在中断服务中又一次调用了同样的函数.当主循环运行到该函数中 ...

  8. sudo,linux 新建账号,并开通ssh登录

    新建账号需要root账号或sudo权限,sudo配置保存在/etc/sudoers文件. sudoers的配置格式一般为: root ALL=(ALL:ALL) ALL %sudo ALL=(ALL: ...

  9. jQuery 求页面加载等待特效

    这个案例是最近写一些前端展示的案例出现的一个问题,特此记录,使用ajax的一些方法和使用的技巧: $.ajax({ url:"", type:"post", d ...

  10. make[2]: *** No rule to make target `/root/.pyenv/versions/anaconda3-2.4.0/lib/libpython3.5m.so', needed by `evaluation.so'. Stop.

    当出现No rule to make target ,肯定是Makefile有问题. 有的makefile是脚本生成的,你得看脚本的配置文件对不对. 我的是这个脚本生成的.发现是Pythondir的配 ...