复制树结构时的java.util.ConcurrentModificationException
我想将一个树结构(源)复制到另一个树结构(目标),但在执行以下方法时得到了java.util.ConcurrentModificationException:复制树结构时的java.util.ConcurrentModificationException,java,exception,recursion,Java,Exception,Recursion,我想将一个树结构(源)复制到另一个树结构(目标),但在执行以下方法时得到了java.util.ConcurrentModificationException: private void mergeNode(TreeLayoutNode source, TreeLayoutNode target){ List<TreeLayoutNode> children = source.getChildren(); if(CollectionUtils.isEmpty(chi
private void mergeNode(TreeLayoutNode source, TreeLayoutNode target){
List<TreeLayoutNode> children = source.getChildren();
if(CollectionUtils.isEmpty(children)){
return;
}
Iterator<TreeLayoutNode> it = children.iterator();
while(it.hasNext()){
**TreeLayoutNode child = it.next();**
TreeLayoutNode copy = new TreeLayoutNode();
copy.setName(child.getName());
copy.setCapacity(child.getCapacity());
copy.setParent(child.getParent());
copy.setChildren(child.getChildren());
copy.setProportions(child.getProportions());
target.addChildNode(copy);
mergeNode(child, copy);
}
}
私有void合并节点(TreeLayoutNode源,TreeLayoutNode目标){
List children=source.getChildren();
if(CollectionUtils.isEmpty(儿童)){
返回;
}
Iterator it=children.Iterator();
while(it.hasNext()){
**TreeLayoutNode子节点=it.next()**
TreeLayoutNode copy=新建TreeLayoutNode();
copy.setName(child.getName());
copy.setCapacity(child.getCapacity());
copy.setParent(child.getParent());
copy.setChildren(child.getChildren());
copy.setPiots(child.getPiots());
target.addChildNode(复制);
合并节点(子节点,副本);
}
}
以“**”开头的代码是发生异常的地方。有人能给出任何提示吗?删除对“copy.setChildren(child.getChildren())”的调用。这将导致对源树中的子级的引用被添加到目标树中,这不是您想要的。递归方法调用将处理填充子对象
您还需要将新节点的父节点设置为新树中的正确节点,而不是调用“copy.setParent(child.getParent())”,后者将父节点引用设置为旧树中的节点。调用可能应该是“copy.setParent(target)”。请添加运行提供的方法的代码,并导致异常。合并节点做什么?@JohnKane
mergeNode
是否发布了代码。。。这是一个递归函数。@Powerlord是的,它旨在递归复制节点。您能突出显示异常发生的行吗?