java.util.ConcurrentModificationException异常解决

来源:互联网 发布:动作数据在n站 编辑:程序博客网 时间:2024/06/14 20:18

当contents由contents = new ArrayList<>();或contents = Collections.synchronizedList(new ArrayList());这两种方式创建时,在contents.remove(str);时便会抛出如标题所示异常。解决方法如下:

List<String> contents = new CopyOnWriteArrayList<>(new ArrayList());//Collections.synchronizedList(new ArrayList<String>());//new ArrayList<>();        for (int i = 0 ; i < 100; i ++)        {            contents.add("list" + i);        }        for (String str: contents)        {            if (str.equals("list20"))            {                contents.remove(str);            }        }
0 0
原创粉丝点击