Java - Exception in thread “main” java.util.ConcurrentModificationException

后端 未结 7 1826
广开言路
广开言路 2021-01-07 14:54

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 st         


        
7条回答
  •  醉酒成梦
    2021-01-07 15:27

    If you want to add to the original ArrayList, then iterate through it on your own:

    final ArrayList arr = hm.get(1);
    final int size = arr.size();
    
    // this will add size number of "hello" strings to ArrayList arr
    for(int i = 0; i < size; ++i){
        // you don't appear to ever use this value
        final String s = arr.get(i);
    
        // do something to arr
        arr.add("hello");
    }
    

提交回复
热议问题