【发布时间】:2012-04-08 21:42:48
【问题描述】:
我收到以下异常
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.TreeMap$PrivateEntryIterator.nextEntry(Unknown Source)
at java.util.TreeMap$KeyIterator.next(Unknown Source)
at Types$AdjList.makeConnected(Types.java:281)
at Main.main(Main.java:56)
在执行以下代码时
public void makeConnected() {
TreeSet<Node> exploredNodes = new TreeSet<Node>();
TreeSet<Node> unexploredNodes = new TreeSet<Node>();
for (Node n : unexploredNodes) {
...
exploredNodes.add(n);
unexploredNodes.remove(n);
...
}
我没有像在 HashMap 中那样使用迭代器,而是需要使用一个可以根据某些条件增长或减少的 Set。 我会接受并给所有答案加分。期待如何回复ConcurrentModificationException这个问题怎么解决 谢谢, 索姆纳特
【问题讨论】:
-
@assylias:好的,我看到 TreeSet 有迭代器
-
@assylias:但是 TreeSet 没有 iterator.remove()。你知道如何使用迭代器从 TreeSet 中删除吗?
-
所有迭代器都有一个 remove 方法。检查上面链接中第一个答案中的第二段代码,并将地图替换为您的集合。
标签: java multithreading collections iterator