How to Convert Array to ArrayList in Java?

0 Flares 0 Flares ×

While writing Java applications we need to convert Array to ArrayList lots of time. Though the process is very simple and straightforward most of the developers end up in iterating the array to form the arraylist.

In this example we will be using Arrays.asList() method to convert the Array to ArrayList.

Java Example to Convert Array to ArrayList

package com.lessonslab.util;

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

public class ConvertArrayToArrayList
public static void main(String args[])
String[] array = {"Test1","Test2","Test3","Test4"};
//Convert to ArrayList
List<String> arrayList = new ArrayList<String>(Arrays.asList(array));
//Lets print the ArrayList


[Test1, Test2, Test3, Test4]

To Generalise: To convert Array of any class to ArrayList you can use the below code. Replace T with the class of which you want to create the ArrayList.

List<T> arrayList = new ArrayList<T>(Arrays.asList(array));

The above code will work for any type of conversion.


+Ayodhyanath Guru holds a B.Tech degree in Electrical Engineering and has worked with various prestigious clients in the IT industry and presently working as a Software Engineer. He is a part time blogger and presently authors the Jafaloo.Com blog. Being a tech enthusiast Guru likes to surf the web and blogs about interesting technical topics like How-To guides, freewares, Tutorials, Software, Gadgets, web applications etc. Apart from blogging he likes coding in Java/J2EE and PHP.

You may also like...

Leave a Reply

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

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×