用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. JS 中的事件绑定、事件监听、事件委托

    事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...

  2. oracle中字符串连接用||

    oracle中字符串连接用|| create or replace procedure testIf(idid number) is v_name stu.name%type; v_age stu.a ...

  3. Foundation ----->NSDictionary

    /*_______________不可变字(NSDictionary)____________*/          //1.字典的创建     //值(value)     NSArray *arr ...

  4. excel解析二维数组结构的excel

    public void fileImport(Ufile ufile) throws Exception { String filePath = ufile.getFilePath(); List&l ...

  5. 机器学习PR:k近邻法分类

    k近邻法是一种基本分类与回归方法.本章只讨论k近邻分类,回归方法将在随后专题中进行. 它可以进行多类分类,分类时根据在样本集合中其k个最近邻点的类别,通过多数表决等方式进行预测,因此不具有显式的学习过 ...

  6. Java数组的一些基本算法

    数组的一些算法问题:  排序:(升序)   选择排序:     求每一轮的最小值:再输出   冒泡排序:     相邻的两个数相比较,把两个数相比较,第一个大于好面的就交换位置   shell排序: ...

  7. MongoDB释放磁盘空间

    1. 先删除旧的文档db.remove({}): 2. 从副本集中移除待清理的成员: (1) 登录副本集中的 Primary 服务器(/opt/mongo1/bin/mongo --port 2701 ...

  8. 把java对象转化为json格式的对象数组

  9. 书单.md

    0823 John Hoskin, An Ilustrated History of Thailand.Asia Books Co., Ltd.2015 0729 Gerald Graff, Cath ...

  10. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中web相关的知识(概述)

    Spring Framework中web相关的知识 1.概述: 参考资料:官网documentation中第22小节内容 关于spring web mvc:  spring framework中拥有自 ...