ConcurrentModificationException while Modifying an ArrayList During Iteration
The reported exception is a ConcurrentModificationException, originating from the attempt to modify an ArrayList, mElements, while iterating over it.
Inside an OnTouchEvent处理程序,使用迭代器对融合的融合有一个循环迭代以检查特定条件:
进行修改(通过添加新元素)。完成迭代:for (Iteratorit = mElements.iterator(); it.hasNext();){ Element element = it.next(); // Check element's position and other conditions... if(element.cFlag){ mElements.add(new Element("crack",getResources(), (int)touchX,(int)touchY)); // ConcurrentModificationException occurs here element.cFlag = false; } }
Alternative Approach:
Another approach is to use an enhanced for-each loop, which iterates over a copy of the list, preventing ConcurrentModificationException:
for (Element element : mElements) { //检查元素的位置和其他条件... 如果(element.cflag){ MELEMENTS.ADD(新元素(“ crack”,getResources(),(int)touchx,(int)touchy)); //没有conturrentModificationException element.cflag = false; } }免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3