for each删除List中的元素出错

来源:互联网 发布:淘宝香烟专卖 编辑:程序博客网 时间:2024/04/28 23:52

转载请标明出处:http://blog.csdn.net/xx326664162/article/details/50883862 文章出自:薛瑄的博客

你也可以查看我的其他同类文章,也会让你有一定的收货!

场景是,需要删除objList中的某几个元素,自然而然,我们会习惯性的写下如下语句:

int i = 0;  for(Object o : objList)  {      if(o == value)     {         objList.remove(i);      }     i++;  }  

报错:

这时你就会发现报 java.util.ConcurrentModificationException 异常,此异常是迭代器抛出的异常,官方说明是:

The Iterators returned by this class’s iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

大概意思是:在用迭代器迭代集合的时候,迭代器一旦创建,就不允许更改集合,如果所迭代的集合(Set或者List)的有修改的话,就会抛出
ConcurrentModificationException异常, 用迭代器自身的remove方法除外…

原理:

用for-each遍历 实际上使用的是Iterator迭代器
Iterator的工作机制:
Iterator是工作在一个独立的线程中,并且拥有一个 mutex锁,就是说Iterator在工作的时候,是不允许被迭代的对象被改变的。Iterator被创建的时候,建立了一个内存索引表(单链表),这 个索引表指向原来的对象,当原来的对象数量改变的时候,这个索引表的内容没有同步改变,所以当索引指针往下移动的时候,便找不到要迭代的对象,于是产生错 误。

List、Set等是动态的,可变对象数量的数据结构,但是Iterator则是单向不可变,只能顺序读取,如果逆向读取,需要重写iterator(),当 Iterator指向的原始数据发生变化时,Iterator自己就迷失了方向。

解决方法:

删除指定的元素应该如何呢?

一. 用一个List 记录要删除的数据,最后removeAll(List);

List<Integer> removeList = new ArrayList()  for(Integer i : intList)  {      if(i == 13)      {          removeList.add(i);      }  }  //最后  if(removeList.size()>0)  {      intList.removeAll(removeList);  }

二.用for循环遍历,原始书写方式

for(int i = 0; i < intList.size(); i++)  {      if(intList.get(i) == 13)      {          intList.remove(i);          //此时要注意,因为list会动态变化不像数组会占位,所以当前索引应该后退一位          i--;      }  }

三.用迭代器自带的remove方法,这也是官方推荐的方法

Iterator <Integer> it = intList.iterator();  while(it.hasNext())  {      if(it.next() == 13)      {          it.remove();      }  }

转载:http://blog.csdn.net/tianlincao/article/details/7006963

0 0
原创粉丝点击