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

Java - 線程“main"中的異常java.util.ConcurrentMod

Java - Exception in thread quot;mainquot; java.util.ConcurrentModificationException(Java - 線程“main中的異常java.util.ConcurrentModificationException)
本文介紹了Java - 線程“main"中的異常java.util.ConcurrentModificationException的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

有什么方法可以在迭代時修改特定鍵的 HashMap 值?

下面給出一個示例程序:

public static void main(String[] args) {哈希映射<整數,數組列表<字符串>>hm = new HashMap<Integer, ArrayList<String>>();ArrayListar = new ArrayList<String>();for(int i=0;i<50;i++){ar.add(Integer.toString(i));}hm.put(1, ar);for(字符串 s:hm.get(1)){hm.get(1).add("你好");}}

拋出錯誤:

線程main"中的異常 java.util.ConcurrentModificationException在 java.util.ArrayList$Itr.checkForComodification(未知來源)在 java.util.ArrayList$Itr.next(未知來源)在 Excp.main(Excp.java:17)

解決方案

檢測到對象的并發修改但不允許修改的方法可能會拋出此異常.

以下代碼導致問題.

for(String s:hm.get(1)){hm.get(1).add("你好");}

您正在迭代和修改相同的內容.通過創建 new ArrayList

來避免這種情況

 ArrayListar1 = new ArrayList();對于(字符串 s:hm.get(1)){ar1.add("你好");}

閱讀這里p>

Is there any way I can modify the HashMap values of a particular key while iterating over it?

A sample program is given below:

public static void main(String[] args) {
    HashMap<Integer,ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>();      
    ArrayList<String> ar = new ArrayList<String>(); 
    for(int i=0;i<50;i++){              
        ar.add(Integer.toString(i));            
    }

    hm.put(1, ar);      

    for(String s:hm.get(1)){
        hm.get(1).add("hello");
    }

}

Error Thrown:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at Excp.main(Excp.java:17)

解決方案

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

Below peice of code is causing the problem.

for(String s:hm.get(1)){
        hm.get(1).add("hello");
    }

You are iterating and modifying the same. Avoid this by creating new ArrayList

  ArrayList<String> ar1 = new ArrayList<String>();

for (String s : hm.get(1)) {
            ar1.add("hello");
        }

have a read here

這篇關于Java - 線程“main"中的異常java.util.ConcurrentModificationException的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 一区二区三区中文字幕 | 亚洲狠狠干 | 成年人视频免费看 | www.av在线| 国产盗摄一区二区 | 羞羞的视频在线观看 | 欧美黄网站| 一区二区三区日韩 | 国产精品嫩草影院桃色 | 亚洲在线免费 | 日本黄色三级视频 | 久草免费福利 | 国产欧美一区二区精品性色超碰 | 视频在线观看一区 | 久久综合亚洲 | 一区二区高清视频 | 特级西西444www大胆免费看 | 黄av在线 | 99热国产| 亚洲精品视频免费 | 日韩欧美国产精品 | 成人午夜在线观看 | 亚洲看片 | 伊人网在线视频 | 欧美成人性生活视频 | 午夜看片 | 日本一级黄色 | 秋霞午夜鲁丝一区二区老狼 | 国产蜜臀av | 国产日韩欧美 | 激情久久网 | 波多野结衣av在线播放 | 亚洲性视频 | 日韩超碰 | 久久久久一区二区 | 国产黄a三级三级看三级 | 国产精品911 | 国产精品自拍小视频 | 福利视频午夜 | 国产一区在线看 | 色爱综合区 |