java.util.ConcurrentModificationException

  1. 用iterator遍历集合时要注意的地方:不可以对iterator相关的地方做添加或删除操作。  
  2.   
  3. 下面用List为例来说明为什么会报 ConcurrentModificationException  这个异常,其它集合类似可以自己思考。  
  4.   
  5.    
  6.   
  7. public static void main(String[] args){  
  8.   List<String> set = new ArrayList<String>();  
  9.   set.add("a10001");  
  10.   set.add("a10002");  
  11.   set.add("a10003");  
  12.   set.add("a10004");  
  13.   set.add("a10005");  
  14.   set.add("a10006");  
  15.   set.add("a10007");  
  16.     
  17.   List<String> del = new ArrayList<String>();  
  18.   del.add("a10003");  
  19.   del.add("a10004");  
  20.   del.add("a10005");  
  21.     
  22.   for(String str : set)  
  23.   {  
  24.    if(del.contains(str))  
  25.    {  
  26.     set.remove(str);  
  27.    }  
  28.   }  
  29.  }  
  30.   
  31.    
  32.   
  33. 运行这段代码的结果  
  34.   
  35.    
  36.   
  37. Exception in thread "main" java.util.ConcurrentModificationException  
  38.  at java.util.AbstractList$Itr.checkForComodification(Unknown Source)  
  39.  at java.util.AbstractList$Itr.next(Unknown Source)  
  40.  at com.debug.Debug.main(Debug.java:28)  
  41.   
  42.    
  43.   
  44. 大家都知道for(String str : set) 这句话实际上是用到了集合的iterator() 方法  
  45.   
  46. 在iterator的时候是产生了一个List的内部类Itr   
  47.   
  48. JDK源码  
  49.   
  50. public Iterator<E> iterator() {  
  51.    return new Itr();  
  52. }  
  53.   
  54. new Itr()时有一个关键性的操作   
  55.   
  56. /** 
  57.   * The modCount value that the iterator believes that the backing 
  58.   * List should have.  If this expectation is violated, the iterator 
  59.   * has detected concurrent modification. 
  60.   */  
  61.  int expectedModCount = modCount;  
  62.   
  63.    
  64.   
  65. 再回头看一下List 的 remove方法  
  66.   
  67.  public boolean remove(Object o) {  
  68.  if (o == null) {  
  69.             for (int index = 0; index < size; index++)  
  70.   if (elementData[index] == null) {  
  71.       fastRemove(index);  
  72.       return true;  
  73.   }  
  74.  } else {  
  75.      for (int index = 0; index < size; index++)  
  76.   if (o.equals(elementData[index])) {  
  77.       fastRemove(index);  
  78.       return true;  
  79.   }  
  80.         }  
  81.  return false;  
  82.     }  
  83.   
  84.    
  85.   
  86. private void fastRemove(int index) {  
  87.         modCount++;  
  88.         int numMoved = size - index - 1;  
  89.         if (numMoved > 0)  
  90.             System.arraycopy(elementData, index+1, elementData, index,  
  91.                              numMoved);  
  92.         elementData[--size] = null// Let gc do its work  
  93.     }  
  94.   
  95.    
  96.   
  97. 再看一下 iterator.next()操作  
  98.   
  99. public E next() {  
  100.             checkForComodification();  
  101.      try {  
  102.   E next = get(cursor);  
  103.   lastRet = cursor++;  
  104.   return next;  
  105.      } catch (IndexOutOfBoundsException e) {  
  106.   checkForComodification();  
  107.   throw new NoSuchElementException();  
  108.      }  
  109.  }  
  110.   
  111.    
  112.   
  113. final void checkForComodification() {  
  114.      if (modCount != expectedModCount)  
  115.   throw new ConcurrentModificationException();  
  116.  }  
  117.   
  118.    
  119.   
  120. 相信看到这儿大家已经应该明白了为什么会出现在这个异常了。  
  121.   
  122. 总结:  
  123.   
  124.   iterator 时 将expectedModCount = modCount 在remove()时 modCount++ 在next()时  
  125.   
  126.  if (modCount != expectedModCount)  
  127.   throw new ConcurrentModificationException();  
  128.   
  129.    
  130.   
  131. 一旦删除或添加元素后 modCount ,expectedModCount 这两个值就会不一致 当next时就会报ConcurrentModificationException   
  132.   
  133.    
  134.   
  135.   
  136. 了解了原由,解决方案就很简单了 在遍历时用一个集合存放要删除的对象 在遍历完后 调用removeAll(Collection<?> c) 就OK了。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值