posts - 72, comments - 66, trackbacks - 0, articles - 0
          在使用Iterator處理Collection時(shí),注意java.util.ConcurrentModificationException。
          1.如果你僅僅是對(duì)collection進(jìn)行遍歷查詢,那么不必?fù)?dān)心什么。
          2.但如果你在遍歷過(guò)程中要對(duì)collection進(jìn)行刪除,那么你就要注意了。
          For example:
          private void testDel() {  
          1.     List<String> list = new ArrayList<String>();  
          2.     for (int i = 0; i < 10; i++) {  
          3.         String str = "td" + i;  
          4.         list.add(str);  
          5.     }  
          6.   
          7.     for (Iterator it = list.iterator(); it.hasNext();) {  
          8.         String str = (String) it.next();  
          9.         if (str.equals("td5")) {  
          10.             // list.remove(str);  // 刪除方法一 
          11.             it.remove();  // 刪除方法二 
          12.         }  
          13.     }  

          上面的代碼運(yùn)行沒(méi)有問(wèn)題,但如果你用“方法一”替代“方法二”,則會(huì)出現(xiàn)java.util.ConcurrentModificationException。
          (用for-each遍歷也會(huì)出個(gè)類似問(wèn)題)
          具體原因是可以看一下先看看List中的remove方法源碼:
          1. public boolean remove(Object o) {  
          2.     if (o == null) {  
          3.         for (int index = 0; index < size; index++)  
          4.             if (elementData[index] == null) {  
          5.                 fastRemove(index);  
          6.                 return true;  
          7.             }  
          8.     } else {  
          9.         for (int index = 0; index < size; index++)  
          10.             if (o.equals(elementData[index])) {  
          11.                 fastRemove(index);  
          12.                 return true;  
          13.             }  
          14.     }  
          15.     return false;  
          16. }  
          17.   
          18. private void fastRemove(int index) {  
          19.     modCount++; // 特別注意這里,這里只增加了modCount的值  
          20.     int numMoved = size - index - 1;  
          21.     if (numMoved > 0)  
          22.         System.arraycopy(elementData, index + 1, elementData, index,  
          23.                 numMoved);  
          24.     elementData[--size] = null; // Let gc do its work  

          接著看。刪除后得到下一個(gè)元素的代碼,it.next():  it為AbstractList的內(nèi)部類Iterator的一個(gè)實(shí)例。
          1. public E next() {  
          2.     checkForComodification();  
          3.     try {  
          4.         E next = get(cursor);  
          5.         lastRet = cursor++;  
          6.         return next;  
          7.     } catch (IndexOutOfBoundsException e) {  
          8.         checkForComodification();  
          9.         throw new NoSuchElementException();  
          10.     }  
          11. }  
          12.   
          13. final void checkForComodification() {  //注意這個(gè)方法
          14.     if (modCount != expectedModCount)  //檢查這兩個(gè)值是否相同
          15.         throw new ConcurrentModificationException();  

          最后看Iterator的remove()方法的源代碼:
          1. public void remove() {  
          2.     if (lastRet == -1)  
          3.         throw new IllegalStateException();  
          4.     checkForComodification();  
          5.     try {  
          6.         AbstractList.this.remove(lastRet);  
          7.         if (lastRet < cursor)  
          8.             cursor--;  
          9.         lastRet = -1;  
          10.         expectedModCount = modCount; // 設(shè)置expectedModCount  
          11.     } catch (IndexOutOfBoundsException e) {  
          12.         throw new ConcurrentModificationException();  
          13.     }  
          14. }  
          15.   
          16. final void checkForComodification() {  
          17.     if (modCount != expectedModCount)  
          18.         throw new ConcurrentModificationException();  

          這下就明白了,list的remove方法只修改了modCount值,而iterator的remove能同步modCount和expectedModCount.



          Feedback

          # re: 使用Iterator 或for-each注意:java.util.ConcurrentModificationException  回復(fù)  更多評(píng)論   

          2010-03-03 11:08 by 羅萊家紡官方網(wǎng)站
          士大夫見說(shuō)道
          主站蜘蛛池模板: 涟水县| 阳高县| 思茅市| 安义县| 杭锦旗| 曲沃县| 平凉市| 吉水县| 通城县| 华坪县| 尼勒克县| 桓台县| 株洲县| 宜宾县| 漳平市| 宝山区| 沂源县| 故城县| 保亭| 大关县| 新邵县| 安义县| 平定县| 扎囊县| 临邑县| 迁西县| 荥经县| 高淳县| 马关县| 阳泉市| 新郑市| 交口县| 鱼台县| 唐山市| 泰和县| 内黄县| 黔西县| 册亨县| 平武县| 客服| 唐山市|