[英]Java, Performing a deep, deep copy of a ArrayList of Nodes that make up a directed graph
我在执行包含Node
对象的Java ArrayList的副本时遇到问题。 这些Node
的Edge
对象的HashSet<Edge>
指向ArrayList中的其他Node
,以形成有向图。 我需要在保持有向图结构的同时复制此ArrayList,以便可以遍历复制的列表,就像使用原始列表一样。
问题是我的深层列表不够“深层”。 当我在下面的方法中复制数组时, Node
对象的副本仍然指向原始数组中的Node,而不是新数组中的Node
。
我该如何更改cloneList
函数,以便执行数组的深层深复制,以便在输出数组中维护有向图结构?
public static ArrayList<Node> cloneList(ArrayList<Node> inList)
{
ArrayList<Node> clonedList = new ArrayList<Node>(inList.size());
for(Node aNode : inList)
{
clonedList.add(new Node(aNode));
}
return clonedList;
}
节点
import java.util.ArrayList;
import java.util.HashSet;
public class Node
{
public String name;
public HashSet<Edge> inEdges;
public HashSet<Edge> outEdges;
public ArrayList<String> deps;
public Node(String name, ArrayList<String> deps) {
this.name = name;
inEdges = new HashSet<Edge>();
outEdges = new HashSet<Edge>();
this.deps = deps;
}
public Node addEdge(Node node){
Edge e = new Edge(this, node);
outEdges.add(e);
node.inEdges.add(e);
return this;
}
@Override
public String toString() {
return name;
}
//Used to copy a given node
public Node(Node inNode)
{
this.name = inNode.name;
this.inEdges = (HashSet<Edge>)inNode.inEdges.clone();
this.outEdges = (HashSet<Edge>)inNode.outEdges.clone();
this.deps = inNode.deps;
}
}
边缘
public class Edge
{
public Node from;
public Node to;
public Edge(Node from, Node to) {
this.from = from;
this.to = to;
}
@Override
public boolean equals(Object obj) {
Edge e = (Edge)obj;
return e.from == from && e.to == to;
}
}
覆盖Node和edge中的克隆函数,以使用所包含对象的深层克隆( obj.clone()
)。 例如,对于Edge
,您可以使用
public Edge clone(){
return new Edge(from.clone(), to.clone());
}
(前提是您为Node提供了克隆功能)。
然后唯一的问题是集合(字符串是不可变的,不需要克隆)。 使用如何克隆ArrayList以及它的内容中提供的解决方案的方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.