这只是一个测试程序(我原来的程序从一个文件中获取数据,所以我省略了,因为它可能人认识我的问题复杂化)深拷贝(链表)
不管怎么说,我试图深拷贝我的对象数据,但我打印复制方法时得到一个“空”?代码有什么问题?这是我们如何使用递归进行深度复制?如果没有,深度复制的任何提示?任何其他方式来保持复制与递归分开?我并不完全确定自己所做的事是否正确,因为我正在重复使用源代码中的复制方法。
主要
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);
}
LinkedList的
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();
}
感谢
请仅发布一小段代码,缩小您面临的问题。 –
没有冒犯,但你为什么使用自己的LinkedList实现而不是java.util? – Rishav
抱歉,您的意思是? – Mathy