Код: Выделить всё
public void testCopyConstructor() {
// Test copy constructor correctly creates a copy of a given list
LinkedList original = new LinkedList();
original.add("one"); original.add("two"); original.add("three");
LinkedList copy = new LinkedList(original);
System.out.println("Copy in Test Case: " + copy.toString());
assertEquals("one two three", copy.toString());
}
Код: Выделить всё
/**
* Copy constructor - copies each element from the given linked list
* into the one being constructed.
* @param l - the linked list being copied from
*/
public LinkedList(LinkedList l) {
// TODO: implement method
// Empty linked list
LinkedList copy = new LinkedList();
current = l.head;
while (current != null) {
copy.add(current.getData());
current = current.getLink();
}
System.out.println("Copy in Constructor: " + copy.toString());
}
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/790 ... onstructor