<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 72, comments - 66, trackbacks - 0, articles - 0
    在使用Iterator處理Collection時,注意java.util.ConcurrentModificationException。
    1.如果你僅僅是對collection進行遍歷查詢,那么不必擔心什么。
    2.但如果你在遍歷過程中要對collection進行刪除,那么你就要注意了。
    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.     }  

    上面的代碼運行沒有問題,但如果你用“方法一”替代“方法二”,則會出現java.util.ConcurrentModificationException。
    (用for-each遍歷也會出個類似問題)
    具體原因是可以看一下先看看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  

    接著看。刪除后得到下一個元素的代碼,it.next():  it為AbstractList的內部類Iterator的一個實例。
    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() {  //注意這個方法
    14.     if (modCount != expectedModCount)  //檢查這兩個值是否相同
    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; // 設置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  回復  更多評論   

    2010-03-03 11:08 by 羅萊家紡官方網站
    士大夫見說道
    主站蜘蛛池模板: 亚洲国产成人久久笫一页| 天天操夜夜操免费视频| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 亚洲av永久无码嘿嘿嘿| 18女人腿打开无遮掩免费| 久久亚洲日韩精品一区二区三区 | 久久精品国产亚洲αv忘忧草| 亚洲香蕉免费有线视频| 亚洲国产成人精品久久| 99精品全国免费观看视频| 亚洲变态另类一区二区三区| 精品国产一区二区三区免费看| 亚洲av无码日韩av无码网站冲| 国产午夜无码视频免费网站| 国产精品久久久久久亚洲小说| 亚洲成人影院在线观看| 国产精品高清免费网站| 亚洲av无码精品网站| 最近免费中文字幕大全免费| 亚洲一区在线观看视频| 国产美女无遮挡免费视频网站| 成人特级毛片69免费观看| 亚洲毛片αv无线播放一区| 久久久久成人片免费观看蜜芽| 亚洲国产成人久久三区| 日本无吗免费一二区| 97在线免费视频| 激情五月亚洲色图| 亚洲精品无码久久久| 免费国产黄网站在线观看视频 | 国产亚洲精品一品区99热| 亚洲黄色免费网址| 亚洲av日韩综合一区久热| 久久伊人亚洲AV无码网站| 精品国产sm捆绑最大网免费站| 亚洲AV无码一区二区三区性色 | 国内大片在线免费看| 成人A毛片免费观看网站| 亚洲一区二区三区四区视频| 亚洲国产综合精品一区在线播放| 少妇太爽了在线观看免费视频|