Java 深度复制(链表)
这只是一个测试程序(我的原始程序从一个文件中获取数据,所以我省略了它,因为它可能会使人们无法理解我的问题) 无论如何,我尝试了深度复制我的对象数据,但是当我打印复制方法时,我得到了一个“null”?代码怎么了?这就是我们使用递归进行深度复制的方式吗?如果没有,有什么深层复制的技巧吗?有没有其他方法让复制和递归分开?老实说,我不完全确定我所做的是否正确,因为我正在重用源代码中的copy方法 主要 “变量str可能尚未初始化” 链接列表Java 深度复制(链表),java,recursion,linked-list,deep-copy,Java,Recursion,Linked List,Deep Copy,这只是一个测试程序(我的原始程序从一个文件中获取数据,所以我省略了它,因为它可能会使人们无法理解我的问题) 无论如何,我尝试了深度复制我的对象数据,但是当我打印复制方法时,我得到了一个“null”?代码怎么了?这就是我们使用递归进行深度复制的方式吗?如果没有,有什么深层复制的技巧吗?有没有其他方法让复制和递归分开?老实说,我不完全确定我所做的是否正确,因为我正在重用源代码中的copy方法 主要 “变量str可能尚未初始化” 链接列表 class LinkedList { // referenc
class LinkedList {
// reference to the head node.
private Node head;
private int listCount;
// LinkedList constructor
public LinkedList() {
// this is an empty list, so the reference to the head node
// is set to a new node with no data
head = new Node(null);
listCount = 0;
}
public void add(Object data) // appends the specified element to the end of this list.
{
Node Temp = new Node(data);
Node Current = head;
// starting at the head node, crawl to the end of the list
while (Current.getNext() != null) {
Current = Current.getNext();
}
// the last node's "next" reference set to our new node
Current.setNext(Temp);
listCount++;// increment the number of elements variable
}
public int size() // returns the number of elements in this list.
{
return listCount;
}
public String toString() {
Node Current = head.getNext();
String output = "";
while (Current != null) {
output += "[" + Current.getData().toString() + "]";
Current = Current.getNext();
}
return output;
}
}
节点
下面是Node类中的copy方法
public static Node copy(Node str) {
if (str == null) {
return null;
}
Node copyFirst = new Node(str.data, null);
copyFirst.next = copy(str.next);
return copyFirst;
}
}
人
class Person {
private String firstName;
private String lastName;
private int age;
public Person() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//Overriding toString to be able to print out the object in a readable way
//when it is later read from the file.
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(firstName);
buffer.append(" ");
buffer.append(lastName);
buffer.append(" ");
buffer.append(age);
buffer.append(" ");
return buffer.toString();
}
谢谢
你期待什么
您需要复制列表,而不是某个虚拟节点。为此,
LinkedList
需要支持复制(或者至少是一种迭代元素的方法)<代码>节点应该是一个对链接列表
的用户完全隐藏的实现细节当您按原样重用节点值时,将创建一个浅层副本。你在你的节点中循环,用相同的值创建新节点,并将新节点链接在一起。您需要保留第一个作为新LinkedList对象的引用
深度复制是指同时克隆数据。如果您的所有对象都实现了
Cloneable
,您只需实现如上所述的克隆,而不是使用相同的值创建新节点,您只需为新节点创建一个值的克隆,瞧,您得到了一个深度副本 请仅发布一小段代码,以缩小您所面临的问题。无意冒犯,但为什么您要使用自己的LinkedList实现而不是java.util实现?对不起,您的意思是?他的意思是已经有了java中的实现这显然是一个单链接列表。Java只有双链表。这很明显,但当我不等于null时。。。它仍然不起作用。“Node copy=Node.copy(str);”表示变量str可能没有初始化好,我看不到任何其他可以初始化它的地方。你是想复制lList
?@Steve:无意冒犯,但也许你需要重新审视变量是什么。是的,我正在尝试复制lList
class Node {
// reference to the next node in the chain,
// or null if there isn't one.
Node next;
// data carried by this node.
// could be of any type you need.
Object data;
// Node constructor
public Node(Object dataValue) {
next = null;
data = dataValue;
}
// another Node constructor if we want to
// specify the node to point to.
public Node(Object dataValue, Node nextValue) {
next = nextValue;
data = dataValue;
}
// these methods should be self-explanatory
public Object getData() {
return data;
}
public void setData(Object dataValue) {
data = dataValue;
}
public Node getNext() {
return next;
}
public void setNext(Node nextValue) {
next = nextValue;
}
public static Node copy(Node str) {
if (str == null) {
return null;
}
Node copyFirst = new Node(str.data, null);
copyFirst.next = copy(str.next);
return copyFirst;
}
class Person {
private String firstName;
private String lastName;
private int age;
public Person() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//Overriding toString to be able to print out the object in a readable way
//when it is later read from the file.
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(firstName);
buffer.append(" ");
buffer.append(lastName);
buffer.append(" ");
buffer.append(age);
buffer.append(" ");
return buffer.toString();
}
//dummy variable
Node str = null;
//deep copy
Node copy = Node.copy(str);
System.out.println(copy);