Hier ist der Konstruktor:
Code: Select all
public SortedLinkedSet(SortedLinkedSet copy) {
if (copy == null) {
this.firstNode = null;
} else{
SortedLinkedSetNode firstNode1 = new SortedLinkedSetNode(copy.getFirstNode().value);
this.firstNode = firstNode1;
// so basically I am chaining elements from "copy" to firstNode1 and then making "this" = to firstNode1.
while (copy.firstNode.next !=null) {
firstNode1.add(copy.getFirstNode().next.value);
this.firstNode = firstNode1;
copy.firstNode = copy.firstNode.next;
// at the end of this loop I have a successful new linkedList with the same value, but "copy" has been changed
}
}
}
Update: Wie Ireeder betonte, und mit einem Test, den ich durchgeführt habe, bin ich mir fast sicher, dass das Problem in der Anweisung vorliegt:
Copy.Firstnode = Copy.Firstnode.Next;SortedLinkedSetNode firstNode = new SortedLinkedSetNode(copy.getFirstNode().value);
this.firstNode=firstNode;
firstNode.add(copy.getFirstNode().next.value);
this.firstNode = firstNode;
firstNode.add(copy.getFirstNode().next.next.value);
this.firstNode = firstNode;
< /code>
Und das funktioniert perfekt (aber ich wusste im Voraus, dass ich mit nur 3 Elementliste teste). Wie würde ich es mit einer Weile Schleife tun, ohne eine solche Anweisung zu verwenden:
copy.firstnode = Copy.Firstnode.Next;>