Skip to content
Related Articles

Related Articles

Improve Article

Copy Elements of One ArrayList to Another ArrayList in Java

  • Last Updated : 19 Jan, 2021

It is the implementation class of List Interface. It allows duplicates objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into getting (index) method. You can add the elements into ArrayList using the add() method.

Syntax of ArrayList Initialization:  

ArrayList<Integer> gfg=new ArrayList<>();

Copying Elements of one ArrayList to another ArrayList

There are two approaches in first you actually just need to pass the reference of one ArrayList to another and in this case, the if you change in one ArrayList value or element then you can see the same change in other ArrayList. The second approach is where you will create actual duplicates means if you change in one ArrayList Element than it will not reflect in the other one.

Approach 1: Using the assignment operator(=)



In this approach, we will simply assign the first ArrayList reference to the second but there is one important aspect to look at here we did not create a new object we simply pointed the second ArrayList to the first one. So if you make a change in the first ArrayList it will reflect in the second one also because you are using the same object. We can also change one value of one ArrayList and can look for the same in the other one whether it is changed or not.

Syntax :

ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=gfg;

Below is the implementation of the above problem statement.

Java




// Java Program for copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        // Assigning the first reference to second
        ArrayList<Integer> gfg2 = gfg;
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list and you can
        // see the same change in the first Arraylist
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}
Output
-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =23
third element of second list =23

Approach 2: Passing in the constructor

In this approach, we will simply pass the first ArrayList in the second ArrayList’s constructor. By using this approach if we change one ArrayList element/value it will not affect the other one, so this is the approach where we actually created the duplicates. We can also change one value of one ArrayList and can look for the same in the other one whether it is changed or not.

Syntax :

ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=new ArrayList<>(gfg);

Below is the implementation of the above problem statement:



Java




// Java Program for  copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        // passing in the constructor
        ArrayList<Integer> gfg2 = new ArrayList<>(gfg);
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list and you can
        // here we will not see the same change in the first
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}
Output
-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =22
third element of second list =23

Approach 3: Adding one by one using add() method

In this approach, we will iterate over each element of the first ArrayList and add that element in the second ArrayList. Here if you change first ArrayList element than it will not change the elements of the second ArrayList. We can also change one value of one ArrayList and can look for the same in the other one whether it is changed or not.

Syntax :

ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=new ArrayList<>();
for(Integer val: gfg){
gfg2.add(val);
}

Below is the implementation of the above problem statement:

Java




// Java Program for  copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        ArrayList<Integer> gfg2 = new ArrayList<>();
  
        // adding element to the second ArrayList
        // by iterating over one by one
        for (Integer value : gfg) {
            gfg2.add(value);
        }
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
  
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list
        // here we will not see the same change in the first
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}
Output
-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =22
third element of second list =23

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :