0

我有一个问题ConcurrentModificationException

我有一个我定义ArrayListComplex类。我添加了两个Complexes,并尝试为每个循环做一个,但我得到了ConcurrentModificationException. 但是,当我删除该行时,我没有收到任何错误。我需要那些初始点(1,0)(-1,0)计算我以后需要的点。

        for (Iterator<Complex> num = dots.iterator(); num.hasNext();) {
            // ConcurrentModificationException
            Complex aComplex = num.next();
            // clone it and clear
            temp.add(new Complex(aComplex));
            dots.clear();
        }
4

2 回答 2

1

迭代时不能修改集合。如果你要移动dots.clear(); 和 temp.clear() 外部迭代;它会得到解决。如果需要,您可以在需要清除这些集合时创建一个标志;迭代结束后,您可以清除它们。

于 2013-01-10T01:40:41.647 回答
0

大多数迭代器实现不允许修改底层结构,除非它具有迭代器本身(remove方法)上定义的语义。

因此,在您在迭代结构时清除结构的所有代码部分中,您将获得一个ConcurrentModificationException.

于 2013-01-10T01:41:32.597 回答