问题描述
我试着从一个ArrayList中删除的项目。的有些时候,的它会弹出一个例外,java.util.ConcurrentModificationException。
Im trying to delete item from a ArrayList. Some times it pops an exception, "java.util.ConcurrentModificationException".
首先,我试图通过array_list_name.remove(I)将其删除,但是失败了,有些人被要求使用迭代器来代替。所以,我目前的code是如下:
First i tried to remove them by array_list_name.remove(i), but it failed and some people were asked to use Iterator instead. So my current code is as follows.
for (Iterator<Collectable> iter = array_list_name.iterator(); iter.hasNext();) {
Collectable s = iter.next();
if (s.equals(array_list_name.get(id))){
iter.remove();
return true;
}
}
和我打电话里面的onDrawarray_list_name鉴于()函数。我的看法是一个SurfaceView。任何一个可以建议我如何删除ArrayList的项目没有得到这个错误。
And i call "array_list_name" inside onDraw() function in view. my view is a SurfaceView. Can any one suggest me how to delete items from ArrayList without getting this error.
推荐答案
尝试使用 java.util.concurrent.CopyOnWriteArrayList中的
而不是的ArrayList
这篇关于如何处理ConcurrentModificationException的Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!