久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Iterator 的 remove 方法實(shí)際上是如何刪除一個(gè)對(duì)象的

How Iterator#39;s remove method actually remove an object(Iterator 的 remove 方法實(shí)際上是如何刪除一個(gè)對(duì)象的)
本文介紹了Iterator 的 remove 方法實(shí)際上是如何刪除一個(gè)對(duì)象的的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我們都知道,在迭代時(shí)從集合中刪除對(duì)象的最安全可能也是唯一安全"的方法是首先檢索 Iterator,執(zhí)行循環(huán)并在需要時(shí)刪除;

We all know that the safest "and probably only safe" way of removing an object from a collection while iterating it, is by first retrieving the Iterator, perform a loop and remove when needed;

Iterator iter=Collection.iterator();
while(iter.hasNext()){
    Object o=iter.next()
    if(o.equals(what i'm looking for)){
        iter.remove();
    }
}

我想了解,但遺憾的是還沒有找到深入的技術(shù)解釋,是如何執(zhí)行此刪除操作,
如果:

What I would like to understand, and unfortunately haven't found a deep technical explanation about, is how this removal is performed,
If:

for(Object o:myCollection().getObjects()){
    if(o.equals(what i'm looking for)){
        myCollection.remove(o);
    }
}

會(huì)拋出一個(gè)ConcurrentModificationException,從技術(shù)上來說"Iterator.remove()是做什么的?它會(huì)移除對(duì)象、中斷循環(huán)并重新開始循環(huán)嗎?

Will throw a ConcurrentModificationException, what does "in technical terms" Iterator.remove() do? Does it removes the object, breaks the loop and restart the loop?

我在官方文檔中看到:

I see in the official documentation:

"刪除當(dāng)前元素.如果有,則拋出 IllegalStateException嘗試調(diào)用 remove() 之前沒有調(diào)用下一個(gè)()."

"Removes the current element. Throws IllegalStateException if an attempt is made to call remove() that is not preceded by a call to next( )."

刪除當(dāng)前元素"部分讓我想到了常規(guī)"循環(huán)中發(fā)生的完全相同的情況 =>(執(zhí)行相等測(cè)試并在需要時(shí)刪除),但為什么迭代器循環(huán) ConcurrentModification 安全?

The part "removes the current element", makes me think of the exact same situation happening in a "regular" loop => (perform equality test and remove if needed), but why is the Iterator loop ConcurrentModification-safe?

推薦答案

Iterator 移除元素的具體方式取決于它的實(shí)現(xiàn),對(duì)于不同的 Collections 可能會(huì)有所不同.絕對(duì)不會(huì)破壞您所處的循環(huán).我剛剛查看了 ArrayList 迭代器的實(shí)現(xiàn)方式,代碼如下:

How exactly Iterator removes elements depends on its implementation, which may be different for different Collections. Definitely it doesn't break the loop you're in. I've just looked how ArrayList iterator is implemented and here's the code:

public void remove() {
    if (lastRet < 0)
        throw new IllegalStateException();
    checkForComodification();

    try {
        ArrayList.this.remove(lastRet);
        cursor = lastRet;
        lastRet = -1;
        expectedModCount = modCount;
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}

因此它檢查并發(fā)修改,使用公共 ArrayList remove 方法刪除元素,并增加列表修改的計(jì)數(shù)器,以便在下一次迭代時(shí)不會(huì)拋出 ConcurrentModificationException.

So it checks for concurrent modifications, removes element using public ArrayList remove method, and increments counter of list modifications so ConcurrentModificationException won't be thrown at next iteration.

這篇關(guān)于Iterator 的 remove 方法實(shí)際上是如何刪除一個(gè)對(duì)象的的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 蜜桃特黄a∨片免费观看 | av国产精品毛片一区二区小说 | 久草精品视频 | 久久久精彩视频 | 国产第二页| 91精品国产一区二区三区 | 精品国产精品国产偷麻豆 | 欧美一级在线观看 | 欧美激情在线精品一区二区三区 | 国产美女黄色片 | 99re视频在线观看 | 亚洲欧洲精品在线 | 精品毛片视频 | 天天操天天玩 | 亚洲aⅴ | 一区二区三区精品在线视频 | 午夜精品一区二区三区在线观看 | 久久久久国产一区二区三区 | 日韩免费网站 | 韩国理论电影在线 | 久久午夜视频 | 大象视频一区二区 | 日韩高清一区二区 | 日韩www | 免费色网址 | 一本岛道一二三不卡区 | 午夜精品一区二区三区在线视频 | 精品不卡 | 国产男女视频网站 | 欧美一级高潮片免费的 | 中文字幕国产精品视频 | 91中文在线观看 | 国产综合久久久久久鬼色 | 欧美成人精品一区二区三区 | 亚洲精品乱码久久久久久按摩观 | 91精品国产日韩91久久久久久 | 91精品久久久久久久久 | 午夜视频网站 | 女女百合av大片一区二区三区九县 | 综合欧美亚洲 | 三级黄色片在线 |