用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. 转换,2D,3D

    一,转换定义: 1,能够改变元素的形状,尺寸,位置 2,转换分两种: 2D转换:只能在X,Y轴发生改变: 例子:旋转(rotate).拉伸(scale).平移(move).倾斜(skew) 3D转换: ...

  2. iOS 获取应用版本信息

    现在许多接口都需要上传应用版本信息,所以呢,这个是必不可少的,可以在进入应用的时候先获取到,然后存在单例中,用的时候直接调用单例就好了,记住这些字符串 NSString *executableFile ...

  3. 微软第四题 给定cost能遍历的最大城市数

    有向图中N*N矩阵 cost:M, 最多可以遍历的结点个数 例如A可以有0->1->2->0->1 代价:2+2+3+2=9<10 输出4 #include <io ...

  4. vi常用命令

    哎,看书上vi命令那么多,真是记不住.记几个常用的备忘吧. 在一般模式下(不可编辑),比如用vi命令打开或创建一个文件,常用的命令如下: 按键 作用 h或← 光标左移动一个字符 j或↓ 光标下移动一个 ...

  5. Android SDK的安装与环境变量配置

    配置Andriod环境变量前提是要先安装好JAVA环境 1.下载Android SDK,点击安装,直接默认路径即可! 下载地址:http://developer.android.com/sdk/ind ...

  6. 动态组合lambda 表达式

    //记录实体集合—动态组合lambda 表达式 Expression<Func<AdEntity, bool>> thirdWhere = p => p.Observer ...

  7. Res_Orders_01

    一.燃尽图展示 二.项目进展 1.讨论选题内容 2.确定项目的版本(Web版) 3.讨论能达到的效果和内容 4.确定编程方面的难点 5.开始制作大概的框架 三.遇到问题 1.不知道怎么部署能达到最好的 ...

  8. Tomcat和JavaWeb笔记

    <iframe src="http://www.xmind.net/embed/XFXh" width="900px" height="540p ...

  9. AIDL和生成的java文件要分开存放,否则生成can't find symbol class

    Android 5.0之后需要AIDL和生成的java文件要分开存放,否则生成can't fin symbol class.但是4.3没有这个限制.被这个弄了一天,跟老大讨论了一下才豁然开朗.

  10. vc++ mfc中拖动效果的实现 借助于CImageList

    拖动是界面编程频繁使用的一个效果,在windows系统下可谓大行其道.纵观时下的应用软件几乎各个都支持各种各样拖动的效果,windows7更是把拖动做到了极致.其实说起来拖动的实现也很简单,对于有句柄 ...