关于用ListIterator遍历,修改,删除ArrayList的同步问题用ListIterator来遍历,修改,删除一个给定的ArrayList,会出现ConcurrentModificationException,就是说ListIterator的结构是第一次运行ArrayListlist.iterator()的时候确定下来的,以后如果remove了ArrayList的item,会出现ConcurrentModificationException,请问如何解决这个问题?code as following:
-----------------------
ListIterator itr = arrayList.listIterator();
while (itr.hasNext())
{
//do something
if (.....)
{
itr.remove();
}
}
这段code会有潜在的ConcurrentModificationException,原因就是我上述说的问题,ListIterator的结构不会因ArrayList而动态变化,请问大家有什么解决方案没有,多谢了
-----------------------
ListIterator itr = arrayList.listIterator();
while (itr.hasNext())
{
//do something
if (.....)
{
itr.remove();
}
}
这段code会有潜在的ConcurrentModificationException,原因就是我上述说的问题,ListIterator的结构不会因ArrayList而动态变化,请问大家有什么解决方案没有,多谢了
for(int i=0;i<list.size();i++)
{
Object o=list.get(i);
//do sth
if(...)
{
list.remove(i);
i--;
}
}