亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在本例中我沒有得到java.util.ConcurrentModificationExcep

為什么在本例中我沒有得到java.util.ConcurrentModificationExcep

喵喵時光機 2019-06-28 17:18:46
為什么在本例中我沒有得到java.util.ConcurrentModificationException?注:我知道Iterator#remove()方法。在下面的代碼示例中,我不明白為什么List.remove在……里面main方法拋ConcurrentModificationException,但是不在remove方法。public class RemoveListElementDemo {         private static final List<Integer> integerList;     static {         integerList = new ArrayList<Integer>();         integerList.add(1);         integerList.add(2);         integerList.add(3);     }     public static void remove(Integer toRemove) {         for(Integer integer : integerList) {             if(integer.equals(toRemove)) {                                 integerList.remove(integer);             }         }     }     public static void main(String... args) {                         remove(Integer.valueOf(2));         Integer toRemove = Integer.valueOf(3);         for(Integer integer : integerList) {             if(integer.equals(toRemove)) {                                 integerList.remove(integer);             }         }     }}
查看完整描述

3 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

原因如下:正如Javadoc中所說的:

這個類的迭代器和listIterator方法返回的迭代器是快速失敗的:如果列表在創建迭代器之后的任何時候進行了結構上的修改,那么除了通過迭代器自己的Remove或Add方法,迭代器將拋出一個ConcurrentModificationException。

此檢查是在next()迭代器的方法(從堆棧跟蹤中可以看到)。但我們會到達next()方法只在hasNext()傳遞為真,這是每個人所調用的,以檢查邊界是否滿足。在刪除方法中,當hasNext()檢查它是否需要返回另一個元素,它將看到它返回了兩個元素,現在,在刪除一個元素之后,列表只包含兩個元素。因此,所有的都是桃子,我們已經完成了迭代。不會對并發修改進行檢查,因為這是在next()方法,該方法從未被調用。

接下來我們進入第二個循環。刪除第二個數字后,hasNext方法將再次檢查是否可以返回更多的值。它已經返回了兩個值,但是列表現在只包含一個。但這里的代碼是:

public boolean hasNext() {
        return cursor != size();}

1!=2,所以我們繼續到next()方法,該方法現在認識到有人一直在處理列表并觸發異常。

希望這能澄清你的問題。

摘要

List.remove()不會扔ConcurrentModificationException當它從列表中移除第二個最后一個元素時。


查看完整回答
反對 回復 2019-06-28
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

的副本中刪除某物的一種方法。Collection(不包括收款本身),如果適用的話。Clone原始集合,以便通過Constructor.

此異常可能由檢測到對象的并發修改的方法引發,而這種修改是不允許的。

對于你的具體情況,首先,我不認為final是一種考慮到您打算修改過去聲明的列表的方法。

private static final List<Integer> integerList;

還可以考慮修改副本而不是原始列表。

List<Integer> copy = new ArrayList<Integer>(integerList);for(Integer integer : integerList) {
    if(integer.equals(remove)) {                
        copy.remove(integer);
    }}


查看完整回答
反對 回復 2019-06-28
?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

前向/迭代器方法在移除項時不起作用。您可以在沒有錯誤的情況下刪除元素,但是當您嘗試訪問已刪除的項時,您將得到一個運行時錯誤。您不能使用迭代器,因為PUSY顯示它會導致ConcurrentModificationException,所以使用正則for循環代替,但是后退一步。

List<Integer> integerList;integerList = new ArrayList<Integer>();integerList.add(1);integerList.add(2);integerList.add(3);int size= integerList.size();//Item to removeInteger remove = Integer.valueOf(3);

解決辦法:

如果要刪除List元素,則按反向順序遍歷數組。只需向后遍歷列表,就可以避免訪問已刪除的項,該項將刪除異常。

//To remove items from the list, start from the end and go backwards through the arrayList//This way if we remove one from the beginning as we go through, then we will avoid getting a runtime error//for java.lang.IndexOutOfBoundsException or java.util.ConcurrentModificationException as when we used the iteratorfor (int i=size-1; i> -1; i--) {
    if (integerList.get(i).equals(remove) ) {
        integerList.remove(i);
    }}


查看完整回答
反對 回復 2019-06-28
  • 3 回答
  • 0 關注
  • 727 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號