Java ArrayList Copy Sample Code

There are two option to copy the contents of one ArrayList to another ArrayList in Java. The ArrayList Class allows you to include an existing ArrayList into the Constructor of the new ArrayList. The second option is to call the addAll() method of ArrayList to add all elements of an existing ArrayList object to another.


ArrayList Copy Constructor

In this example, we have a filled ArrayList called jcdArrayList1 and we want to copy all the elements into jcdArrayList2. You can create ArrayList jcdArrayList2 and fill it with the contents by giving the Constructor the argument of jcdArrayList1. Both ArrayList objects will need to be defined with the same data type, String in this sample.

//Create new array list and instantiate it with contents of another array list
ArrayList<String> jcdArrayList2 = new ArrayList<String>(jcdArrayList1);


ArrayList addAll() Method

In this example, we use the addAll() method of ArrayList class to add the contents of jcdArrayList1 to ArrayList jcdArrayList3.

//Add all elements of jcdArrayList1 to this array list
jcdArrayList3.addAll(jcdArrayList1);


Java ArrayList Copy Source Code

This is the full working sample Java code that show all methods to create a copy of an existing ArrayList into a new ArrayList object.

package com.jcd.java.tutorials;

import java.util.ArrayList;

public class JavaArrayListCopy 
{

	public static void main(String[] args) 
	{

		//Create an empty array list object
		ArrayList<String> jcdArrayList1 = new ArrayList<String>();
		
		//Add elements to the array list
		jcdArrayList1.add("JavaCodeDepot.com");
		jcdArrayList1.add("Java Samples");
		jcdArrayList1.add("Java Tutorials");
		
		//Size of array list
		System.out.println("jcdArrayList1 size: "+jcdArrayList1.size());
		
		//Print out elements of jcdArrayList1
		for(String jcdString : jcdArrayList1)
		{
			System.out.println(jcdString);
		}
		
		//Create new array list and instantiate it with contents of another array list
		ArrayList<String> jcdArrayList2 = new ArrayList<String>(jcdArrayList1);
		
		//Size of array list
		System.out.println("\njcdArrayList2 size: "+jcdArrayList2.size());
		
		//Print out elements of jcdArrayList2
		for(String jcdString : jcdArrayList2)
		{
			System.out.println(jcdString);
		}
		
		//Create new empty array list 
		ArrayList<String> jcdArrayList3 = new ArrayList<String>();
		
		//Add all elements of jcdArrayList1 to this array list
		jcdArrayList3.addAll(jcdArrayList1);
		
		//Size of array list
		System.out.println("\njcdArrayList3 size: "+jcdArrayList3.size());
		
		//Print out elements of jcdArrayList2
		for(String jcdString : jcdArrayList3)
		{
			System.out.println(jcdString);
		}
	}

}

Java Copy ArrayList Output

jcdArrayList1 size: 3
JavaCodeDepot.com
Java Samples
Java Tutorials

jcdArrayList2 size: 3
JavaCodeDepot.com
Java Samples
Java Tutorials

jcdArrayList3 size: 3
JavaCodeDepot.com
Java Samples
Java Tutorials



Concluding Comments

I use the ArrayList Copy Constructor when I initially need to make an exact copy of an existing ArrayList and use the addAll() method on the fly in my code when need to create a new copy of an exisitng ArrayList.



Java References:

Add a Comment

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