【发布时间】:2016-10-08 09:01:49
【问题描述】:
我试图做我的链接列表的深层副本,称为DictionaryNode,但我无法在显示方法中显示它的内容,因为它始终为空。为什么 DictinaryNode temp 始终为 null ?如果我尝试分配 temp = head 工作,但 temp = copy 没有。
public class ListOfNodes {
public class DictionaryNode {
protected String word;
private int level;
private DictionaryNode next;
private int space = 0;
public void displayCopy() {
DictionaryNode temp = copy.next;
while( temp != null ) {
System.out.println(temp.word)
temp = temp.next;
}
}
public DictionaryNode( String word, int level ) {
this.word = word;
this.level = level;
next = null;
}
}
private DictionaryNode head = null;
public DictionaryNode copy = null;
//used to do deep copy
public void Clone() {
DictionaryNode temp = head.next;
while( temp != null ) {
copy = new DictionaryNode( temp.word , temp.level );
copy = copy.next;
temp = temp.next;
}
}
public void displayCopy() {
DictionaryNode temp = copy.next;
while( temp != null ) {
Sytem.out.println(temp.word)
temp = temp.next;
}
}
【问题讨论】:
-
你永远不会给
head赋值。另外,我认为您的克隆方法应该返回对克隆字典的引用。 -
head 是通过用户从文件中读取来初始化的,但问题是如果我尝试分配 head 的 temp 引用它可以工作并显示所有内容但是如果我尝试 temp = copy 不起作用
-
如果是这样,那么您发布的代码并不是您实际运行的代码。
标签: java linked-list clone