2015-10-18 194 views
-2

这只是一个测试程序(我原来的程序从一个文件中获取数据,所以我省略了,因为它可能人认识我的问题复杂化)深拷贝(链表)

不管怎么说,我试图深拷贝我的对象数据,但我打印复制方法时得到一个“空”?代码有什么问题?这是我们如何使用递归进行深度复制?如果没有,深度复制的任何提示?任何其他方式来保持复制与递归分开?我并不完全确定自己所做的事是否正确,因为我正在重复使用源代码中的复制方法。

主要

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(); 
} 

感谢

+1

请仅发布一小段代码,缩小您面临的问题。 –

+1

没有冒犯,但你为什么使用自己的LinkedList实现而不是java.util? – Rishav

+0

抱歉,您的意思是? – Mathy

回答

0
//dummy variable 
Node str = null; 

//deep copy 
Node copy = Node.copy(str); 
System.out.println(copy); 

什么迪你期望吗?

您需要复制列表,而不是某个虚拟节点。为此,LinkedList需要支持复制(或至少一种方式来遍历其元素)。 Node应该是一个对LinkedList的用户完全隐藏的实现细节。

+0

那么这是显而易见的,但是当我不等于null ...它仍然无法正常工作。 “节点拷贝= Node.copy(str);”说变量str可能没有初始化 – Mathy

+0

嗯,我没有看到任何其他地方你可能会初始化它。你是否想要复制'lList'? – Svante

+0

@Steve:没有冒犯,但也许你需要重新审视一个变量。 – Svante

0

浅拷贝是当您按原样重用节点值时。例如。您循环遍历节点,创建具有相同值的新节点并将新节点链接在一起。您需要保留第一个作为新的LinkedList对象的引用。

深拷贝是数据被克隆的时候。如果你的所有对象都实现了Cloneable,那么你只需按照上面的描述来实现克隆,而不是使用相同的值创建新节点,而只是为新节点创建一个克隆值,并获得深层副本。