日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Iterator 或for-each注意:java.util.ConcurrentModificationException

發布時間:2025/3/17 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Iterator 或for-each注意:java.util.ConcurrentModificationException 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用Iterator 或for-each注意:java.util.ConcurrentModificationException

Posted on 2010-03-02 12:22 Fingki.li 閱讀(1360) 評論(1) ?編輯 ?收藏 所屬分類: About development 在使用Iterator處理Collection時,注意java.util.ConcurrentModificationException。
1.如果你僅僅是對collection進行遍歷查詢,那么不必擔心什么。
2.但如果你在遍歷過程中要對collection進行刪除,那么你就要注意了。
For example:
private?void?testDel()?{??
  • ????List<String>?list?=?new?ArrayList<String>();??
  • ????for?(int?i?=?0;?i?<?10;?i++)?{??
  • ????????String?str?=?"td"?+?i;??
  • ????????list.add(str);??
  • ????}??
  • ??
  • ????for?(Iterator?it?=?list.iterator();?it.hasNext();)?{??
  • ????????String?str?=?(String)?it.next();??
  • ????????if?(str.equals("td5"))?{??
  • ????????????//?list.remove(str);??// 刪除方法一?
  • ????????????it.remove();??// 刪除方法二?
  • ????????}??
  • ????}??
  • }?
  • 上面的代碼運行沒有問題,但如果你用“方法一”替代“方法二”,則會出現java.util.ConcurrentModificationException。
    (用for-each遍歷也會出個類似問題)
    具體原因是可以看一下先看看List中的remove方法源碼:
  • public?boolean?remove(Object?o)?{??
  • ????if?(o?==?null)?{??
  • ????????for?(int?index?=?0;?index?<?size;?index++)??
  • ????????????if?(elementData[index]?==?null)?{??
  • ????????????????fastRemove(index);??
  • ????????????????return?true;??
  • ????????????}??
  • ????}?else?{??
  • ????????for?(int?index?=?0;?index?<?size;?index++)??
  • ????????????if?(o.equals(elementData[index]))?{??
  • ????????????????fastRemove(index);??
  • ????????????????return?true;??
  • ????????????}??
  • ????}??
  • ????return?false;??
  • }??
  • ??
  • private?void?fastRemove(int?index)?{??
  • ????modCount++;?//?特別注意這里,這里只增加了modCount的值??
  • ????int?numMoved?=?size?-?index?-?1;??
  • ????if?(numMoved?>?0)??
  • ????????System.arraycopy(elementData,?index?+?1,?elementData,?index,??
  • ????????????????numMoved);??
  • ????elementData[--size]?=?null;?//?Let?gc?do?its?work??
  • }?
  • 接著看。刪除后得到下一個元素的代碼,it.next():? it為AbstractList的內部類Iterator的一個實例。
  • public?E?next()?{??
  • ????checkForComodification();??
  • ????try?{??
  • ????????E?next?=?get(cursor);??
  • ????????lastRet?=?cursor++;??
  • ????????return?next;??
  • ????}?catch?(IndexOutOfBoundsException?e)?{??
  • ????????checkForComodification();??
  • ????????throw?new?NoSuchElementException();??
  • ????}??
  • }??
  • ??
  • final?void?checkForComodification()?{? //注意這個方法
  • ????if?(modCount?!=?expectedModCount)? //檢查這兩個值是否相同
  • ????????throw?new?ConcurrentModificationException();??
  • }?
  • 最后看Iterator的remove()方法的源代碼:
  • public?void?remove()?{??
  • ????if?(lastRet?==?-1)??
  • ????????throw?new?IllegalStateException();??
  • ????checkForComodification();??
  • ????try?{??
  • ????????AbstractList.this.remove(lastRet);??
  • ????????if?(lastRet?<?cursor)??
  • ????????????cursor--;??
  • ????????lastRet?=?-1;??
  • ????????expectedModCount?=?modCount;?//?設置expectedModCount??
  • ????}?catch?(IndexOutOfBoundsException?e)?{??
  • ????????throw?new?ConcurrentModificationException();??
  • ????}??
  • }??
  • ??
  • final?void?checkForComodification()?{??
  • ????if?(modCount?!=?expectedModCount)??
  • ????????throw?new?ConcurrentModificationException();??
  • }?
  • 這下就明白了,list的remove方法只修改了modCount值,而iterator的remove能同步modCount和expectedModCount. 新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

    總結

    以上是生活随笔為你收集整理的使用Iterator 或for-each注意:java.util.ConcurrentModificationException的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。