使用iterator和iterator.remove()时出现ConcurrentModificationException [英] ConcurrentModificationException when using iterator and iterator.remove()
本文介绍了使用iterator和iterator.remove()时出现ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private int checkLevel(String bigWord, Collection<String> dict, MinMax minMax)
{
/*value initialised to losing*/
int value = 0;
if (minMax == MinMax.MIN) value = 1;
else value = -1;
boolean go = true;
Iterator<String> iter = dict.iterator();
while(iter.hasNext())
{
String str = iter.next();
Collection<Integer> inds = naiveStringSearch(bigWord, str);
if(inds.isEmpty())
{
iter.remove();
}
for (Integer i : inds)
{
MinMax passin = minMax.MIN;
if (minMax == MinMax.MIN) passin = minMax.MAX;
int value2 = checkLevel(removeWord(bigWord, str, i), dict, passin);
if (value2 == -1 && minMax == minMax.MIN)
{
value = -1;
go = false;
}
if (value2 == 1 && minMax == minMax.MAX)
{
value = 1;
go = false;
}
}
if (go == false) break;
}
return value;
}
错误:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
at java.util.HashMap$KeyIterator.next(HashMap.java:845)
at aStringGame.Main.checkLevel(Main.java:67)
at aStringGame.Main.test(Main.java:117)
at aStringGame.Main.main(Main.java:137)
这里有什么问题?
推荐答案
某处某处正在修改 dict
。我怀疑它可能会在这个调用中发生:
Something somewhere is modifying dict
. I suspect it might be happening inside this call:
int value2 = checkLevel(removeWord(bigWord, str, i), dict, passin);
^^^^
编辑基本上,会发生什么递归调用 checkLevel()
通过另一个迭代器修改 dict
。这使得外部迭代器的快速失败行为成为可能。
edit Basically, what happens is that the recursive call to checkLevel()
modifies dict
through another iterator. This makes the outer iterator's fail-fast behaviour to kick in.
这篇关于使用iterator和iterator.remove()时出现ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文