DictionaryNode
というリンクリストのディープコピーを作成しようとしていましたが、常にnullであるため、表示方法でコンテンツを表示できませんでした。なぜDictinaryNodeのtempは常にnullですか?私はtemp = head workを割り当てようとしますが、temp = copyは割り当てません。あなたがコピーされたコンテンツのためnext
フィールドを割り当てることはありませんあなたのClone
方法でLinkedListディープコピーjava
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;
}
}
「ヘッド」には決して値を割り当てません。また、私はあなたのクローンメソッドは、クローンの辞書への参照を返す必要がありますと思う。 –
ヘッドはユーザーからのファイル読み込みで初期化されますが、ヘッドの温度参照を割り当ててすべてのコンテンツを表示しようとすると問題が発生しますが、テンポラリを試してみるとコピーは機能しません – Anny
もしそうなら、あなたが実際に走っていることはありません。 –