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

并發修改異常 (Java)

ConcurrentModificationException (Java)(并發修改異常 (Java))
本文介紹了并發修改異常 (Java)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

Exception in thread "main" java.util.ConcurrentModificationException
Squash the PC dirties the room Violet. The room's state is now dirty
Lily the animal growls
The Animal Lily left the room and goes to Green through the west door.
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
        at java.util.HashMap$KeyIterator.next(HashMap.java:828)
        at homework5.Room.critReactRoomStateChange(Room.java:76)
        at homework5.PC.play(PC.java:121)
        at homework5.Main.main(Main.java:41)
Java Result: 1

這是我收到的錯誤.

我的方法看起來像

public void critReactRoomStateChange(String command, PC pc) {
    Creature temp = null;
    Iterator iterator = getCreatures().keySet().iterator();
    while (iterator.hasNext()) {
        String names = iterator.next().toString();
        if (!(getCreatures().get(names) instanceof PC)) {
            temp = getCreatures().get(names);
            if (temp != null) {
                temp.reactStateChange(command, pc);
                temp.checkNewRoom();
            }
        }
    }
} 

所以我的理解是,這意味著我要在迭代器完成之前更改它的大小,這就是你得到的錯誤.這是正確的,因為 reactStateChange 之一是要從 hashMap 中刪除對象.如何安全地執行此操作,以便在刪除某些內容時讓迭代器提前知道,這樣我就可以避免此錯誤.提前致謝.如果需要更多詳細信息,我很樂意滿足您的要求.

So what I understand is this means I'm changing the size of the iterator before it is finished and this is the error you get. This is true as one of the reactStateChange is for an object to be removed out of the hashMap. How do I do this safely so that when I remove something it lets the Iterator know ahead of time so I can avoid this error. Thanks in advance. If more details are needed I'd be glad to meet your requests.

推薦答案

從底層集合中刪除元素并繼續迭代的唯一安全方法是使用 的 com/javase/6/docs/api/java/util/Iterator.html#remove%28%29" rel="noreferrer">remove() 方法迭代器.這將刪除 Iteratornext() 方法返回的最后一個元素.

The only safe way to remove an element from an underlying collection and continue the iteration is to use the remove() method of the Iterator. This removes the last element returned by the next() method of the Iterator.

在您的情況下,這似乎需要將 Iterator 傳遞給執行修改的方法(或使其成為實例字段,如 Map 對象是已經).

In your case, it appears that this would require passing the Iterator to the method that performs the modification (or make it an instance field, like the Map object is already).

這篇關于并發修改異常 (Java)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: h片在线播放 | 欧美亚洲成人网 | 澳门永久av免费网站 | 国产剧情一区二区三区 | 亚洲欧美一区二区三区国产精品 | 亚洲电影一级片 | 一级黄色淫片 | 欧美日韩高清一区二区三区 | 狠狠亚洲| 精品久久久久久久久久久 | www午夜视频 | 欧美精品一区二区三区四区五区 | 国产91视频播放 | 欧美激情国产精品 | 一区二区三区小视频 | 伊人在线 | 日本综合在线观看 | 99精品一区二区 | 国产精品美女久久久久aⅴ国产馆 | 欧美日韩最新 | 中国一级大毛片 | 欧洲视频一区二区 | 国产成人精品免费 | 999精品在线 | 黄色大全免费看 | 久久成人综合 | 国产欧美一区二区三区久久手机版 | 亚洲日日| www.日日干 | 国产一区二区三区免费观看视频 | 一区视频在线播放 | 亚洲一区 | 91传媒在线观看 | 日韩精品一区二区三区免费观看 | 欧美精品一区免费 | 一区二区三区四区免费视频 | 午夜欧美一区二区三区在线播放 | 久久福利网站 | 欧美成人一区二区三区 | 综合久久av | 亚洲aⅴ |