这只是一个测试程序(我的原始程序从文件中获取数据,所以我省略了它,因为它可能会使人们无法理解我的问题(
无论如何,我尝试深度复制我的对象数据,但在打印复制方法时得到"null"? 代码有什么问题? 这就是我们使用递归进行深度复制的方式吗? 如果没有,有什么深度复制的提示吗?除了递归之外,还有其他方法可以保留副本吗?我不完全确定我所做的是否正确,因为我正在重用来自源的复制方法。
主要
public static void main(String[] args) {
Person person = new Person();
person.setFirstName("James");
person.setLastName("Ryan");
person.setAge(19);
Person personTwo = new Person();
personTwo.setFirstName("Steve");
personTwo.setLastName("rivera");
personTwo.setAge(22);
LinkedList lList = new LinkedList();
// add elements to LinkedList
lList.add(person);
lList.add(personTwo);
//node
Node str;
"变量 str 可能尚未初始化"
//deep copy
Node copy = Node.copy(str);
System.out.println(copy);
}
链接列表
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;
}
}
节点
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;
}
这是 Node 类中的复制方法
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);
你期待什么?
您需要复制列表,而不是某个虚拟节点。 为此,LinkedList
需要支持复制(或者至少是一种迭代其元素的方法(。 Node
应该是对LinkedList
用户完全隐藏的实现细节。
浅拷贝是指按原样重用节点值。 例如,您遍历节点,使新节点具有相同的值并将新节点链接在一起。您需要保留第一个作为新 LinkedList 对象的引用。
深层复制是指同时克隆数据。如果您的所有对象都实现了Cloneable
您只需如上所述实现克隆,而不是使用相同的值创建新节点,您只需为新节点创建值的克隆,瞧,您得到了一个深层副本。