【发布时间】:2014-12-06 18:23:31
【问题描述】:
我有以下代码引发 ConcurrentModificationException。 有人能解释一下为什么会这样吗?
public void foo(ArrayList<Bet> bets)
Iterator it1 = bets.iterator();
while(it1.hasNext())
Bet b1 = (Bet) bets.next()
Iterator it2 = bets.iterator();
while(it2.hasNext())
if(bet1.equals(bet2))
it2.remove();
it1.remove(); //ConcurrentModificationException thrown here
是不是每次 iterator.next() 调用我只能调用一次 iterator.remove(), 并且在下一次调用 iterator.next() 之前调用 remove 两次会导致这种情况?
任何帮助将非常感谢。
【问题讨论】:
标签: java iterator iteration concurrentmodification