Clone a List in Java

Cloning a list in Java using the addAll() method involves creating a copy of an existing list. To do this, you start by adding elements to an original list. Then, you use the addAll() method to copy all the elements from the original list to a new list.

Clone a List in Java

The new list becomes a separate copy, and any modifications made to it won’t affect the original list, demonstrating that the two lists are independent. This approach is useful when you need a duplicate of a list but want to keep the original list unchanged.

reference: https://www.geeksforgeeks.org/how-to-clone-a-list-in-java/

code:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Step 1: Create an empty list named firstList
        ArrayList<String> firstList = new ArrayList<>();
        
        // Step 2: Add elements "sun", "moon", and "star" to firstList
        firstList.add("sun");
        firstList.add("moon");
        firstList.add("star");
        
        // Step 3: Create a new empty list named clonedList
        ArrayList<String> clonedList = new ArrayList<>();
        
        // Step 4: Copy all elements from firstList to clonedList using addAll()
        clonedList.addAll(firstList);
        
        // Step 5: Print firstList and clonedList to show they are identical
        System.out.println("First List: " + firstList);
        System.out.println("Cloned List: " + clonedList);
        
        // Step 6: Change the first element of clonedList from "sun" to "Z"
        clonedList.set(0, "Z");
        
        // Step 7: Print the modified clonedList to show the change
        System.out.println("Modified Cloned List: " + clonedList);
        
        // Step 8: Print firstList again to confirm it remains unchanged
        System.out.println("Original List remains unchanged: " + firstList);
        
        // Observation: clonedList is a separate list with its own changes
    }
}

Explanation:

1. ArrayList<String> firstList = new ArrayList<>();
Creates an empty ArrayList named firstList to store String elements.
2. firstList.add(“sun”);
Adds the element “sun” to firstList. Similarly, “moon” and “star” are added.
3. ArrayList<String> clonedList = new ArrayList<>();
Creates another empty ArrayList named clonedList.
4. clonedList.addAll(firstList);
Copies all elements from firstList to clonedList. The two lists now contain identical elements.
5. System.out.println(“First List: ” + firstList);
Prints the firstList to verify its elements.
6. System.out.println(“Cloned List: ” + clonedList);
Prints the clonedList to verify its elements, confirming it’s identical to firstList.
7. clonedList.set(0, “Z”);
Modifies the first element of clonedList to “Z”.
8. System.out.println(“Modified Cloned List: ” + clonedList);
Prints the modified clonedList to show that its first element has been changed.
9. System.out.println(“Original List remains unchanged: ” + firstList);
Prints the firstList again to confirm it is unaffected by the changes made to clonedList.

Output:

First List: [sun, moon, star]
Cloned List: [sun, moon, star]
Modified Cloned List: [Z, moon, star]
Original List remains unchanged: [sun, moon, star]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top