久久久久久久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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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

下面給出一個示例程序:

public static void main(String[] args) {哈希映射<整數(shù),數(shù)組列表<字符串>>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)

解決方案

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

以下代碼導(dǎo)致問題.

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

您正在迭代和修改相同的內(nèi)容.通過創(chuàng)建 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

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

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

相關(guān)文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉(zhuǎn)換為 Map)
Getting data from JSON(從 JSON 獲取數(shù)據(jù))
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉(zhuǎn)換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應(yīng)一個HashSet創(chuàng)建一個HashMap.出發(fā)點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 福利一区二区在线 | 亚洲女人的天堂 | 亚洲电影一区二区三区 | 亚洲成av人片在线观看无码 | 久久在线看 | 精品91久久 | 欧美天堂| 中文字字幕在线中文乱码范文 | 91精品国产综合久久精品 | 日韩精品a在线观看图片 | 99九九久久 | 亚洲精品1| 欧美男男videos| 国产精品久久久久久久久久久久冷 | 黄网站免费在线观看 | 国产精品久久久久aaaa九色 | 日本精品一区 | 国产四虎| 在线观看免费高清av | 欧美一区二区三区免费电影 | 激情六月丁香婷婷 | 亚洲欧美一区二区三区1000 | 青青草综合网 | av黄色在线播放 | 午夜欧美| 亚洲激情一区二区 | 精品视频亚洲 | 日韩成人在线免费视频 | 日本人做爰大片免费观看一老师 | 亚洲欧美中文日韩在线v日本 | 天堂网色 | 欧美一区日韩一区 | 欧美日韩精品中文字幕 | 一级毛片视频 | 国产精品久久久久久 | 自拍 亚洲 欧美 老师 丝袜 | 国产激情片在线观看 | 久久精品天堂 | 精品久久久久久亚洲精品 | 色站综合 | 国产中文区二幕区2012 |