Java program to copy elements of one ArrayList to another ArrayList

It is the implementation class of the List interface. It allows to repeat objects/elements and maintain the insertion order. You can get the elements that exist in the ArrayList by its index, and now you need to pass it to the Getting (index) method. You can use the add() method to add elements to the ArrayList.

Syntax of ArrayList Initialization:  

ArrayList gfg=new ArrayList<>();

Copying Elements of one ArrayList to another ArrayList

There are two ways first, you actually only need to pass a reference to one ArrayList to another, in this case, if you change one ArrayList value or element, then you can see the same change in the other ArrayList. The second method is that you will create the position of the actual duplicate, which means that if you change one ArrayList element, it will not be reflected in the other element.

Approach 1: Using the assignment operator(=)

In this method, we will simply assign the first ArrayList reference to the second, but there is an important aspect to note here, we did not create a new object, we just point the second ArrayList to the first. Therefore, if you make a change in the first ArrayList, it will also be reflected in the second ArrayList because you are using the same object. We can also change one value of an ArrayList, and can find the same value in another value, whether it changes or not.

Syntax :

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

Below is the implementation of the above problem statement.

 
// 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 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 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 method, we will simply pass the first ArrayList in the constructor of the second ArrayList. By using this method, if we change one ArrayList element/value, it will not affect the other element/value, so this is how we actually create duplicates. We can also change one value of an ArrayList, and can find the same value in another value, whether it changes or not.

Syntax :

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

Below is the implementation of the above problem statement:

// 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 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 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 method, we will iterate through each element of the first ArrayList and add that element to the second ArrayList. Here, if you change the first ArrayList element, it will not change the second ArrayList element. We can also change one value of an ArrayList, and we can find the same value in another value, whether it changes or not.

Syntax :

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

Below is the implementation of the above problem statement:

 
// 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 gfg = new ArrayList<>();

		// adding elements to first ArrayList
		gfg.add(10);
		gfg.add(21);
		gfg.add(22);
		gfg.add(35);

		ArrayList 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

 

Submit Your Programming Assignment Details