How to Convert Array to ArrayList in Java?

This is one of the popular question in the java world. I have encountered this situation a lot of times in my life which made me to write up this article on converting a Java Array to ArrayList. Almost everybody knows that the Arrays are used to store primitive datatypes and ArrayList is used to store the Objects. Lets see the different ways to convert.

1) Using Arrays.asList() method

Look into the below code, we have array called metals which consist of the elements like “Gold”,”Silver”,”Platinum”, now lets convert this array to arraylist (metalsList)

package com.javainterviewpoint;

import java.util.Arrays;
import java.util.List;

public class Convert_Array_to_ArrayList 
{
	public static void main(String args[])
	{
		String[] metals ={"Gold","Silver","Platinum"};
		List metalsList = Arrays.asList(metals);
		System.out.println("Converted List :"+metalsList);
	}
}

Running the above code we have the array converted to a list

Converted List :[Gold, Silver, Platinum]

Now the metalsList will have all the entries of the array. But this way of conversion have a slight drawback, the returned list is the list view of the array and hence the list size will be of fixed size and hence you will not be able add a element  the list.

When we try to add a element this will throw you exception. Lets try that too.

package com.javainterviewpoint;

import java.util.Arrays;
import java.util.List;

public class Convert_Array_to_ArrayList 
{
	public static void main(String args[])
	{
		String[] metals ={"Gold","Silver","Platinum"};
		List metalsList = Arrays.asList(metals);
		System.out.println(metalsList);
		metalsList.add("Titanium");
		System.out.println(metalsList);
	}
}

Exception occured

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(Unknown Source)
	at java.util.AbstractList.add(Unknown Source)
	at com.javainterviewpoint.Convert_Array_to_ArrayList.main(Convert_Array_to_ArrayList.java:13)

Even most important thing is that when you change the element of the list it will get changed in the actual array also

package com.javainterviewpoint;

import java.util.Arrays;
import java.util.List;

public class Convert_Array_to_ArrayList 
{
	public static void main(String args[])
	{
		String[] metals ={"Gold","Silver","Platinum"};
		List metalsList = Arrays.asList(metals);
		System.out.println("Actual List :"+metalsList);
		metalsList.set(1, "Titanium");
		System.out.println("Modified List :"+metalsList);
		System.out.println("Modified Array Element :"+metals[1]);
	}
}

The element at index 1 will be replaced with Titanium in both list and array

Actual List :[Gold, Silver, Platinum]
Modified List :[Gold, Titanium, Platinum]
Modified Array Element :Titanium

2) Array to ArrayList using Collections.addAll() method

This is the most popular and flexible way of conversion

package com.javainterviewpoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Convert_Array_to_ArrayList 
{
	public static void main(String args[])
	{
		String[] metals ={"Gold","Silver","Platinum"};
		List metalsList = new ArrayList();
		Collections.addAll(metalsList,metals);
		System.out.println("Added ArrayList :"+metalsList);
	}
}

This way may not be as fast as the above one. But this creates a copy of the array to the arraylist so you will be allow to modify the list the way you like.

addAll can be used in two different ways

Collection.addAll(metalsList,metals) (or) Collections.addAll(metalsList,”Gold”,”Silver”,”Platinum”)

3) Using Arrays.asList() method in a different way

This way is almost the same as the Point 1 but if you follow this way you will be allowed to make modifications to the arraylist

package com.javainterviewpoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Convert_Array_to_ArrayList 
{
	public static void main(String args[])
	{
		String[] metals ={"Gold","Silver","Platinum"};
		List metalsList = new ArrayList(Arrays.asList(metals));
		System.out.println("New ArrayList :"+metalsList);
		metalsList.add("Titanium");
		System.out.println("Modified ArrayList"+metalsList);
	}
}

Leave a Reply

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