问题是我有这个方法createNode()
在树中创建一个节点,然后如果它是叶节点,则将其添加到 ArrayList<Tree> treeLeaves
中, 我在浏览 treeLeaves
时调用了这个方法ArrayList 像这样:
Iterator<Tree> iter = treeLeaves.iterator();
while (iter.hasNext()) {
iter.next().createNode();
}
或者像这样:
For (Tree cursor : treeLeaves) {
cursor.createNode();
}
但我一直有这个异常(exception):
Exception in thread "main" java.util.ConcurrentModificationException
即使将下面的代码放在 snychronized(treeLeaves){}
中集团。
P.S:我不知道这是否有用但是;这是一棵 n 树。
最佳答案
你需要一个 ConcurrentList...
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
阅读Is there a concurrent List in Java's JDK?
此外,您不能在浏览数组列表时更改它...相反,您可以根据需要使用 tempoaray 内存中的缓冲区来编辑当前列表。
关于Java - 在浏览时将元素添加到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34521810/