java.util.ConcurrentModificationException错误

来源:互联网 发布:windows访问共享文件夹 编辑:程序博客网 时间:2024/05/18 03:19
  • 错误原因-在使用遍历的同时,对List对象进行了remove、add等改变list.size()的操作。
for(String s:list_string){            if(s=="aoe")                list_string.remove(s);        }
  • 错误信息已经指明错误产生在checkForComodification()方法,想知道本质原因,可以查看ArrayList下该方法已经里面变量(modCount、expectedModCount)的定义。
Exception in thread "main" java.util.ConcurrentModificationException    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)    at java.util.ArrayList$Itr.next(Unknown Source)    at com.tm.collections.ListInterfaces.main(ListInterfaces.java:17)
  • 修正方法 :使用迭代器来代替List操作,如下:
Iterator<?> iterator=list_string.iterator();        while (iterator.hasNext()) {            if(iterator.next()=="aoe")                iterator.remove();        }
0 0
原创粉丝点击