第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

為什么在本例中我沒有得到j(luò)ava.util.ConcurrentModificationExcep

為什么在本例中我沒有得到j(luò)ava.util.ConcurrentModificationExcep

喵喵時光機 2019-06-28 17:18:46
為什么在本例中我沒有得到j(luò)ava.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貢獻(xiàn)1833條經(jīng)驗 獲得超4個贊

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

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

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

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

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

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

希望這能澄清你的問題。

摘要

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


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

TA貢獻(xiàn)1815條經(jīng)驗 獲得超6個贊

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

此異??赡苡蓹z測到對象的并發(fā)修改的方法引發(fā),而這種修改是不允許的。

對于你的具體情況,首先,我不認(rèn)為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);
    }}


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

TA貢獻(xiàn)1825條經(jīng)驗 獲得超4個贊

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

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元素,則按反向順序遍歷數(shù)組。只需向后遍歷列表,就可以避免訪問已刪除的項,該項將刪除異常。

//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);
    }}


查看完整回答
反對 回復(fù) 2019-06-28
  • 3 回答
  • 0 關(guān)注
  • 720 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號