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

帶有迭代器的 java.util.ConcurrentModificationException

java.util.ConcurrentModificationException with iterator(帶有迭代器的 java.util.ConcurrentModificationException)
本文介紹了帶有迭代器的 java.util.ConcurrentModificationException的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我知道是否會嘗試通過簡單的循環(huán)從集合中刪除,我會得到這個異常:java.util.ConcurrentModificationException.但我正在使用迭代器,它仍然會產(chǎn)生這個異常.知道為什么以及如何解決它嗎?

I know if would be trying to remove from collection looping through it with the simple loop I will be getting this exception: java.util.ConcurrentModificationException. But I am using Iterator and it still generates me this exception. Any idea why and how to solve it?

HashSet<TableRecord> tableRecords = new HashSet<>();

...

    for (Iterator<TableRecord> iterator = tableRecords.iterator(); iterator.hasNext(); ) {
        TableRecord record = iterator.next();
        if (record.getDependency() == null) {
            for (Iterator<TableRecord> dependencyIt = tableRecords.iterator(); dependencyIt.hasNext(); ) {
                TableRecord dependency = dependencyIt.next(); //Here is the line which throws this exception
                if (dependency.getDependency() != null && dependency.getDependency().getId().equals(record.getId())) {
                    tableRecords.remove(record);
                }
            }
        }
    }

推薦答案

你必須使用 iterator.remove() 而不是 tableRecords.remove()

You must use iterator.remove() instead of tableRecords.remove()

只有在迭代器中使用 remove 方法時,才能刪除要迭代的列表中的項目.

You can remove items on a list on which you iterate only if you use the remove method from the iterator.

當(dāng)您創(chuàng)建迭代器時,它會開始計算應(yīng)用于集合的修改.如果迭代器檢測到一些修改沒有使用它的方法(或者在同一個集合上使用另一個迭代器),它不能再保證它不會在同一個元素上傳遞兩次或跳過一個,所以它拋出這個異常

When you create an iterator, it starts to count the modifications that were applied on the collection. If the iterator detects that some modifications were made without using its method (or using another iterator on the same collection), it cannot guarantee anymore that it will not pass twice on the same element or skip one, so it throws this exception

這意味著您需要更改代碼,以便僅通過 iterator.remove 刪除項目(并且只有一個迭代器)

It means that you need to change your code so that you only remove items via iterator.remove (and with only one iterator)

列出要刪除的項目,然后在完成迭代后將其刪除.

make a list of items to remove then remove them after you finished iterating.

這篇關(guān)于帶有迭代器的 java.util.ConcurrentModificationException的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
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 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 手机av在线 | 国产1区2区3区 | 国产一级久久久久 | 欧美精品久久久久 | 日韩国产免费观看 | 日韩在线观看一区 | 国产成人免费视频网站视频社区 | 欧美v在线| 国产精品一区二区电影 | www成人啪啪18| 欧美一级欧美三级在线观看 | 欧美日韩久久 | 亚洲精品www | 精品一区二区三区在线观看国产 | 日韩精品一区二区三区在线播放 | 欧美伊人| 黄色av免费网站 | 午夜国产 | 成人日b视频 | 免费观看一区二区三区毛片 | 日韩欧美在线视频 | 日韩看片 | 国产丝袜人妖cd露出 | 国产欧美在线视频 | 韩国av一区二区 | 美女国产一区 | 欧美精品日韩精品国产精品 | 精品一区二区在线观看 | 九九九久久国产免费 | 在线亚州 | 看亚洲a级一级毛片 | 91视频网| 日本三级网址 | 涩涩视频网站在线观看 | 国产激情精品视频 | 一a级片| 精精精精xxxx免费视频 | 亚洲一区二区三区在线 | 欧美综合一区二区 | 久久久久99| 日日夜夜天天久久 |