Cer*_*ner 5 java iterator arraylist
我在一个arraylist上运行一个迭代器,并在条件为真时尝试删除一个项目.
我有以下代码:
String item = (String) model.getElementAt(selectedIndices[i]);
Iterator it = p.eggMoves.iterator();
while(it.hasNext())
{
String text = (String) it.next();
if ( text.equals(item) )
{
it.remove();
p.eggMoves.remove(selectedIndices[i]);
model.removeElementAt(selectedIndices[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
现在这段代码工作正常,该项从p对象和jlist中删除,但它在it.next()行引发"ConcurrentModificationException"异常.
我该如何解决这个问题?
Bra*_*raj 12
只需it.remove()在迭代时使用即可删除该项目.
下面的行导致问题
p.eggMoves.remove(selectedIndices[i]);
Run Code Online (Sandbox Code Playgroud)
您想要通过一次又一次删除相同的项目(即索引i)来做什么?
| 归档时间: |
|
| 查看次数: |
20554 次 |
| 最近记录: |