Introduction
The clone()
method in Java's LinkedList
class provides a way to create a copy of the list. It’s important to understand that this isn’t just about creating a new object; it’s about duplicating the structure and elements within the list itself. This is useful when you want to work with a separate version of your linked list without modifying the original.
Syntax
public Object clone()
Parameters
The clone()
method doesn't take any parameters. It creates a copy of the current linked list.
Parameter | Description |
---|---|
None | No input parameters are required. |
Return Value
The clone()
method returns an object that is a copy of the original linked list. This returned object will be another instance of LinkedList
, containing all the same elements and with the same initial capacity.
Examples
Example 1: Creating a Shallow Copy
This example demonstrates how to use the clone()
method. We create an original linked list, then clone it. Notice that the cloned list shares references to the same objects as the original list (shallow copy).
import java.util.LinkedList;
public class LinkedListCloneExample {
public static void main(String[] args) {
LinkedList<String> originalList = new LinkedList<>();
originalList.add("Apple");
originalList.add("Banana");
originalList.add("Cherry");
LinkedList<String> clonedList = (LinkedList<String>) originalList.clone();
System.out.println("Original List: " + originalList);
System.out.println("Cloned List: " + clonedList);
}
}
Original List: [Apple, Banana, Cherry]
Cloned List: [Apple, Banana, Cherry]
Explanation: The `clone()` method creates a new LinkedList object. However, the elements themselves (in this case, Strings like "Apple", "Banana", and "Cherry") are not copied. Instead, both lists share references to the same String objects in memory. This is what we call a shallow copy.
Example 2: Modifying the Original List
This example illustrates that modifying the original list after cloning *will* affect the cloned list because they both reference the same underlying elements (due to the shallow copy).
import java.util.LinkedList;
public class LinkedListCloneExample2 {
public static void main(String[] args) {
LinkedList<String> originalList = new LinkedList<>();
originalList.add("Apple");
originalList.add("Banana");
originalList.add("Cherry");
LinkedList<String> clonedList = (LinkedList<String>) originalList.clone();
System.out.println("Original List before modification: " + originalList);
System.out.println("Cloned List before modification: " + clonedList);
originalList.set(0, "Mango"); // Modify the first element of the original list
System.out.println("Original List after modification: " + originalList);
System.out.println("Cloned List after modification: " + clonedList);
}
}
Original List before modification: [Apple, Banana, Cherry]
Cloned List before modification: [Apple, Banana, Cherry]
Original List after modification: [Mango, Banana, Cherry]
Cloned List after modification: [Mango, Banana, Cherry]
Explanation: When we changed the value at index 0 of `originalList` from "Apple" to "Mango", the change was also reflected in `clonedList`. This is because both lists point to the same String object that now holds the value "Mango".
Example 3: Cloning a List with Custom Objects
This example shows what happens when you have custom objects in your LinkedList. The shallow copy behavior still applies; only the structure of the list is copied, not the objects themselves.
import java.util.LinkedList;
class MyObject {
String name;
public MyObject(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyObject{\"" + name + "\"";
}
}
public class LinkedListCloneExample3 {
public static void main(String[] args) {
LinkedList<MyObject> originalList = new LinkedList<>();
originalList.add(new MyObject("Object 1"));
originalList.add(new MyObject("Object 2"));
LinkedList<MyObject> clonedList = (LinkedList<MyObject>) originalList.clone();
System.out.println("Original List: " + originalList);
System.out.println("Cloned List: " + clonedList);
// Modify the name of an object in the original list
originalList.get(0).name = "Modified Object 1";
System.out.println("Original List after modification: " + originalList);
System.out.println("Cloned List after modification: " + clonedList);
}
}
Original List: [MyObject{'Object 1'}, MyObject{'Object 2'}]
Cloned List: [MyObject{'Object 1'}, MyObject{'Object 2'}]
Original List after modification: [MyObject{'Modified Object 1'}, MyObject{'Object 2'}]
Cloned List after modification: [MyObject{'Modified Object 1'}, MyObject{'Object 2'}]
Explanation: Again, because the `clone()` method performs a shallow copy, changes to the internal state of objects referenced by both lists are reflected in *both* lists. Here, changing `name` of an object in `originalList` also changed it in `clonedList`.