参考:http://blog.csdn.net/androidboy365/article/details/50540202/

解决方案

// 1 使用Iterator提供的remove方法,用于删除当前元素

 for(Iterator<string> it = myList.iterator(); it.hasNext();) {

     String value = it.next();

      if(value.equals("3")) {

          it.remove(); // ok

     }

}

System. out.println( "List Value:"  + myList.toString());

 // 2 建一个集合,记录需要删除的元素,之后统一删除            

List<string> templist = newArrayList<string>();

 for(String value : myList) {

      if(value.equals("3")) {

          templist.remove(value);

     }

}

 // 可以查看removeAll源码,其中使用Iterator进行遍历

myList.removeAll(templist);

System. out.println( "List Value:"  + myList.toString());       

  // 3. 使用线程安全CopyOnWriteArrayList进行删除操作

List<string> myList = newCopyOnWriteArrayList<string>();

myList.add("1");

myList.add("2");

myList.add("3");

myList.add("4");

myList.add("5");

Iterator<string> it = myList.iterator();

 while(it.hasNext()) {

     String value = it.next();

      if(value.equals("3")) {

          myList.remove("4");

          myList.add("6");

          myList.add("7");

     }

}

System. out.println( "List Value:"  + myList.toString());

 // 4. 不使用Iterator进行遍历,需要注意的是自己保证索引正常

 for(inti = 0; i < myList.size(); i++) {

     String value = myList.get(i);

     System. out.println( "List Value:"  + value);

      if(value.equals("3")) {

          myList.remove(value); // ok

          i--;// 因为位置发生改变,所以必须修改i的位置

     }

}

System. out.println( "List Value:"  + myList.toString());

  

单线程情况下列出4种解决方案,但是在多线程情况下仅有第4种方案才能在多线程情况下不出现问题。

List<string> myList = newCopyOnWriteArrayList<string>();

 myList.add("1");

 myList.add("2");

 myList.add("3");

 myList.add("4");

 myList.add("5");

newThread(newRunnable() {

     @Override

     publicvoidrun() {

          for(String string : myList) {

               System.out.println("遍历集合 value = "  + string);

               try{

                    Thread.sleep(100);

               }catch(InterruptedException e) {

                    e.printStackTrace();

               }

          }

     }

}).start();

newThread(newRunnable() {

     @Override

     publicvoidrun() {

          for(inti = 0; i < myList.size(); i++) {

               String value = myList.get(i);

               System.out.println("删除元素 value = "  + value);

           if(value.equals("3")) {

                myList.remove(value);

                i--;// 注意                          

           }

           try{

                    Thread.sleep(100);

               }catch(InterruptedException e) {

                    e.printStackTrace();

               }

          }

     }

}).start();

  

List remove及ConcurrentModificationException异常的更多相关文章

  1. 集合遍历remove时ConcurrentModificationException异常

    1.集合遍历时候,有时候需要remove或add操作,这时候遍历方式可能会影响程序运行 例如: @Test public void test1() { List<Integer> intL ...

  2. Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  3. Java并发编程:Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  4. java集合--java.util.ConcurrentModificationException异常

    ConcurrentModificationException 异常:并发修改异常,当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常.一个线程对collection集合迭代,另一个线程对Co ...

  5. 【转】Java ConcurrentModificationException 异常分析与解决方案--还不错

    原文网址:http://www.2cto.com/kf/201403/286536.html 一.单线程 1. 异常情况举例 只要抛出出现异常,可以肯定的是代码一定有错误的地方.先来看看都有哪些情况会 ...

  6. 【转】ConcurrentModificationException异常解决办法 --不错

    原文网址:http://blog.sina.com.cn/s/blog_465bcfba01000ds7.html 1月30日java.util.ConcurrentModificationExcep ...

  7. 【转】Java ConcurrentModificationException异常原因和解决方法

    原文网址:http://www.cnblogs.com/dolphin0520/p/3933551.html Java ConcurrentModificationException异常原因和解决方法 ...

  8. ConcurrentModificationException异常解决办法

    今天在写一个带缓存功能的访问代理程序时出现了java.util.ConcurrentModificationException异常,因为该异常是非捕获型异常而且很少见,所以费了些时间才找到问题所在,原 ...

  9. 修改List报ConcurrentModificationException异常原因分析

    使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析 在使用Iterator来迭代遍历List的时候如果修改该List对象,则会报jav ...

随机推荐

  1. 使用HTML5的Notification API制作web通知的教程(转)

    var notification=new Notification(‘Notification Title',{ body:'Your Message' }); 上面的代码构造了一个简陋的通知栏.构造 ...

  2. Mysql 数据库字符类型详解

    MySQL 中提供了多种对字符数据的存储类型,不同的版本可能有所差异.以5.0 版本为例,MySQL 包括了CHAR.VARCHAR.BINARY.VARBINARY.BLOB.TEXT.ENUM 和 ...

  3. Jquery.Treeview+Jquery UI制作Web文件预览

    效果图: 前台Html: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="D ...

  4. 12. Min Stack【medium】

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  5. 第7章 Iptables与Firewalld防火墙。

    第7章 Iptables与Firewalld防火墙.     Chapter7_听较强节奏的音乐能够让您更长时间的投入在学习中. <Linux就该这么学> 00:00/00:00     ...

  6. 02、Quick Start for Windows phone

    在使用这个 SDK 提供的功能前,必须先添加类库的引用到你的工程里.参考: Download and add the libraries to the project. 定义你的 XAML 的 UI ...

  7. jvm默认垃圾收集器

    jdk1.7 默认垃圾收集器Parallel Scavenge(新生代)+Parallel Old(老年代) jdk1.8 默认垃圾收集器Parallel Scavenge(新生代)+Parallel ...

  8. docker + ubuntun 安装show doc

    基本安装步骤 Ubuntu Docker 安装 Docker 支持以下的 Ubuntu 版本: Ubuntu Precise 12.04 (LTS) Ubuntu Trusty 14.04 (LTS) ...

  9. dp之多重背包hdu1114

    题目很水,不多说......... #include<stdio.h> int main() { long t,n,m,a,i,j,dp[10005],vol[505],jizhi[505 ...

  10. Extjs,实现树形结构的总结

    工作总结,用extjs.mybatis.springMVC实现树形显示班级 前台extjs实现树形代码如下: xtype : 'combotree', fieldLabel : '部门名称', nam ...