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

帶有迭代器的 java.util.ConcurrentModificationException

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

問題描述

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

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.

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

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.

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

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

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
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)
主站蜘蛛池模板: 国产亚洲精品一区二区三区 | 亚洲午夜精品视频 | 欧美成人一区二区 | 91视频在线网站 | 婷婷福利 | 亚洲日本免费 | 国产精品久久久久久久久免费樱桃 | 国产在线精品一区二区三区 | 天天干狠狠干 | 久久在线 | 日本一区二区在线视频 | 国产一区二区三区四区在线观看 | 国产ts人妖系列高潮 | 中文字幕视频免费 | 亚洲成人一区二区 | 一区二区三区四区视频 | 国产一级视屏 | 国产精品欧美一区二区三区不卡 | 日韩精品视频一区二区三区 | 婷婷五月色综合 | 一级黄色片免费在线观看 | 亚洲一区二区精品视频 | 成人精品在线观看 | 中文字幕在线视频免费观看 | 日韩免费| 亚洲成人av在线播放 | 免费视频成人国产精品网站 | 国产精品久久精品 | av日韩在线播放 | 免费人成在线观看网站 | 91精品国产综合久久久久久 | 正在播放国产精品 | h视频在线免费 | 日韩欧美三级 | 三级成人片 | 成人一区二区三区 | 波多野结衣一区二区 | 国产视频线观看永久免费 | 免费久久久久久 | 一区二区三区视频在线 | 中文字幕免费视频 |