By Ron Tuffin

2008-10-01 14:38:32 8 Comments

I have an array that is initialized like:

Element[] array = {new Element(1), new Element(2), new Element(3)};

I would like to convert this array into an object of the ArrayList class.

ArrayList<Element> arraylist = ???;


@Chris 2020-07-06 08:13:56

Hi you can use this line of code , and it's the simplest way

 new ArrayList<>(Arrays.asList(myArray));

or in case you use Java 9 you can also use this method:

List<String> list = List.of("Hello", "Java"); 
List<Integer> list = List.of(1, 2, 3);

@Andrii Abramov 2016-12-23 12:31:28

Since Java 8 there is an easier way to transform:

import java.util.List;    
import static;

public static <T> List<T> fromArray(T[] array) {

@Suparno Saha 2020-06-02 17:13:35

This is the first and the easiest method

new ArrayList<T>(Arrays.asList(myArray));

Another method

//declare myarray
ArrayList <String> ar = new ArrayList<String> ();
for(String s : myArray){

This is iterating through the array and adding each element separately

The kind of for loop used here is called enhanced for loop

@Vikrant Kashyap 2016-03-30 06:48:14

  1. If we see the definition of Arrays.asList() method you will get something like this:

     public static <T> List<T> asList(T... a) //varargs are of T type. 

    So, you might initialize arraylist like this:

     List<Element> arraylist = Arrays.asList(new Element(1), new Element(2), new Element(3));

    Note : each new Element(int args) will be treated as Individual Object and can be passed as a var-args.

  2. There might be another answer for this question too.
    If you see declaration for java.util.Collections.addAll() method you will get something like this:

    public static <T> boolean addAll(Collection<? super T> c, T... a);

    So, this code is also useful to do so

    Collections.addAll(arraylist, array);

@Tom 2008-10-01 14:39:12

new ArrayList<>(Arrays.asList(array));

@Calum 2008-10-01 14:41:47

Yep. And in the (most common) case where you just want a list, the new ArrayList call is unecessary as well.

@Pool 2011-06-29 15:18:20

@Luron - just use List<ClassName> list = Arrays.asList(array)

@Code Jockey 2011-09-26 19:04:54

@Calum and @Pool - as noted below in Alex Miller's answer, using Arrays.asList(array) without passing it into a new ArrayList object will fix the size of the list. One of the more common reasons to use an ArrayList is to be able to dynamically change its size, and your suggestion would prevent this.

@markbaldy 2012-11-23 05:28:47

I'd like to point out that the new ArrayList<Element>(...) part is necessary if you want to perform mutative (like remove) operations with an iterator.

@Adam 2013-01-23 03:28:53

Arrays.asList() is a horrible function, and you should never just use its return value as is. It breaks the List template, so always use it in the form indicated here, even if it does seem redundant. Good answer.

@user3458 2013-02-20 15:48:05

A caveat: I just run into a misbehavior of Java 6: when Element is a primitive (e.g. the array is int[]), asList creates List<int[]> .

@lbalazscs 2013-02-21 21:13:19

@Adam Arrays.asList() is a very useful method when you want a list backed by a specific array. For example Collections.shuffle(Arrays.asList(myarray)) shuffles the original array.

@Adam 2013-02-22 05:08:01

@lbalazscs true, but I would say that's pretty close to a hack (even if the shuffle method was intended to be used that way), because you're basically tricking it into thinking it's dealing with a List when it's not. Handy, but I still say it's a horrible function. My argument is basically that the asList() method is misleading because it looks like it returns a List representation of the given array, when in fact it returns an only-partially-implemented List representation.

@lbalazscs 2013-02-22 09:41:09

@Adam Please study the javadoc for java.util.List. The contract for add allows them to throw an UnsupportedOperationException.… Admittedly, from an object-oriented perspective it is not very nice that many times you have to know the concrete implementation in order to use a collection - this was a pragmatic design choice in order to keep the framework simple.

@AndrewF 2013-03-13 06:18:45

@Arkadiy It is not a misbeavior that Arrays.asList(a).get(0) == a when a is an int[]. This is because of how the asList(Object...) varargs resolve. An object of type int[] is an Object, not an Object[], so Arrays.asList is being passed an array of Object with one element: an array of int.

@user3458 2013-03-15 13:57:57

@AndrewF I understand why this happens. It's just breaking DWIM for me, is all.

@tuxdna 2013-07-08 07:09:04

This doesn't compile when array is of type int[] and the equivalent usecase is like so: List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArray));

@Mr_and_Mrs_D 2014-04-09 23:55:42

Arrays.asList(array).remove(key) will throw:Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove( at ... - dunno how AbstractList got in there but you do not want it around when calling remove

@RenniePet 2015-02-28 09:24:24

For the case when the array is of a primitive type like int[] see this question and answers:…

@Bruce Wayne 2015-05-27 14:09:31

On a side note, List<Element> list = new ArrayList<>(Arrays.asList(array)); would've sufficed. The diamond operator will do the type inference for you.

@user253751 2015-10-01 02:34:58

@Mr_and_Mrs_D Probably because the list returned by Arrays.asList is of a class that inherits from AbstractList and doesn't implement remove (which is because you can't remove stuff from an array)

@Jingguo Yao 2016-12-30 09:08:57

This does not work for Java 1.8. In Java 1.8, Arrays.asList(array) will create a list whose only element is array. The resulted list does not contain array's elements.

@muzzlator 2017-11-29 01:10:03

Something I just found out (for open jdk at least) is that Arrays.asList(array) creates a lightweight list wrapper around the array but the corresponding toArray call creates a new copy of the array. The call to new ArrayList(Collection) calls this toArray method and then creates another defensive copy, which means one additional array allocation. You can avoid all these by using Lists.newArrayList in guava or create new ArrayList(array.length) and then copy all the elements across.

@Datz 2018-03-27 06:45:39

With Java 9 you should use java.util.List.of(array) which gives you an immutable list (instance of internal class AbstractImmutableList). You can use new ArrayList<>(List.of(array)) if you need a modifiable list. see List

@Singh123 2019-12-04 07:59:42

Below code seems nice way of doing this.

new ArrayList<T>(Arrays.asList(myArray));

@Arpan Saini 2019-09-07 20:36:28

Given Object Array:

Element[] array = {new Element(1), new Element(2), new Element(3) , new Element(2)};

Convert Array to List:

    List<Element> list =;

Convert Array to ArrayList

    ArrayList<Element> arrayList =

Convert Array to LinkedList

    LinkedList<Element> linkedList =

Print List:

    list.forEach(element -> {





@Kaplan 2019-08-02 05:50:11

the lambda expression that generates a list of type ArrayList<Element>
(1) without an unchecked cast
(2) without creating a second list (with eg. asList())

ArrayList<Element> list = Stream.of( array ).collect( Collectors.toCollection( ArrayList::new ) );

@Himanshu Dave 2019-05-19 11:20:27

Java 8’s Arrays class provides a stream() method which has overloaded versions accepting both primitive arrays and Object arrays.

/**** Converting a Primitive 'int' Array to List ****/

int intArray[] = {1, 2, 3, 4, 5};

List<Integer> integerList1 =;

/**** 'IntStream.of' or '' Gives The Same Output ****/

List<Integer> integerList2 = IntStream.of(intArray).boxed().collect(Collectors.toList());

/**** Converting an 'Integer' Array to List ****/

Integer integerArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

List<Integer> integerList3 =;

@Devratna 2019-03-28 11:42:43

Use below code

Element[] array = {new Element(1), new Element(2), new Element(3)};
ArrayList<Element> list = (ArrayList) Arrays.asList(array);

@Kavinda Pushpitha 2018-12-03 11:32:10

Use the following code to convert an element array into an ArrayList.

Element[] array = {new Element(1), new Element(2), new Element(3)};

ArrayList<Element>elementArray=new ArrayList();
for(int i=0;i<array.length;i++) {

@Abojemyeg 2017-01-05 18:32:55

as all said this will do so

 new ArrayList<>(Arrays.asList("1","2","3","4"));

and the common newest way to create array is observableArrays

ObservableList: A list that allows listeners to track changes when they occur.

for Java SE you can try

FXCollections.observableArrayList(new Element(1), new Element(2), new Element(3));

that is according to Oracle Docs

observableArrayList() Creates a new empty observable list that is backed by an arraylist. observableArrayList(E... items) Creates a new observable array list with items added to it.

Update Java 9

also in Java 9 it's a little bit easy:

List<String> list = List.of("element 1", "element 2", "element 3");

@Ali Dehghani 2016-04-17 16:58:35

Java 9

In Java 9, you can use List.of static factory method in order to create a List literal. Something like the following:

List<Element> elements = List.of(new Element(1), new Element(2), new Element(3));

This would return an immutable list containing three elements. If you want a mutable list, pass that list to the ArrayList constructor:

new ArrayList<>(List.of(// elements vararg))

JEP 269: Convenience Factory Methods for Collections

JEP 269 provides some convenience factory methods for Java Collections API. These immutable static factory methods are built into the List, Set, and Map interfaces in Java 9 and later.

@tquadrat 2019-07-05 12:31:16

List.of() will not return an instance of java.util.ArrayList, as requested in the original question. Therefore only the second option is a valid answer.

@rashedcs 2017-07-04 07:22:33

We can easily convert an array to ArrayList. We use Collection interface's addAll() method for the purpose of copying content from one list to another.

 Arraylist arr = new Arraylist();

@AjahnCharles 2017-07-10 10:06:06

This is less efficient than the accepted 9 year old answer.

@Tamoghna Chowdhury 2017-10-06 20:26:11

One of ArrayLists constructors accepts a ? extends Collection<T> argument, making the call to addAll redundant.

@MarekM 2017-04-11 12:20:45

In Java 9 you can use:

List<String> list = List.of("Hello", "World", "from", "Java");
List<Integer> list = List.of(1, 2, 3, 4, 5);

@Dorian Gray 2018-08-03 20:57:30

Note that this is not an ArrayList, as it was explicitely asked.

@Adit A. Pillai 2017-06-07 12:14:25

You can do it in java 8 as follows

ArrayList<Element> list = (ArrayList<Element>);

@Dorian Gray 2018-07-03 17:45:31

Downvoted because that cast looks very dangerous. nothing specifies that the type of list that is returned is actually an ArrayList, as the javadoc states: "There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned"

@Dorian Gray 2018-08-03 20:53:53

If you want to explicitely create an ArrayList, try this: ArrayList<String> list =‌​st::new));

@Sumit Das 2017-07-25 06:03:11

Already everyone has provided enough good answer for your problem. Now from the all suggestions, you need to decided which will fit your requirement. There are two types of collection which you need to know. One is unmodified collection and other one collection which will allow you to modify the object later.

So, Here I will give short example for two use cases.

  • Immutable collection creation :: When you don't want to modify the collection object after creation

    List<Element> elementList = Arrays.asList(array)

  • Mutable collection creation :: When you may want to modify the created collection object after creation.

    List<Element> elementList = new ArrayList<Element>(Arrays.asList(array));

@Priyanka 2017-09-09 08:57:54

List<Element> elementList = Arrays.asList(array) creates a wrapper over the original array which makes original array available as List. Hence a wrapper object is created, nothing gets copied from the original array. Therefore, operations like add or remove elements are not allowed.

@Tamoghna Chowdhury 2017-10-06 20:24:50

Note that your "immutable collection" is not really immutable - the List returned by Arrays.asList is just a wrapper over the original array, and allows individual items to be accessed and modified via get and set. You should probably clarify that you mean "not add or remove elements" instead of "immutable", which means to not change at all.

@A1m 2017-07-10 02:21:29

If the array is of a primitive type, the given answers won't work. But since Java 8 you can use:

int[] array = new int[5];;

@PixelMaster 2018-06-10 19:55:46

this solution doesn't seem to work with char array, either.

@yegor256 2017-06-26 12:19:51

You can create an ArrayList using Cactoos (I'm one of the developers):

List<String> names = new StickyList<>(
  "Scott Fitzgerald", "Fyodor Dostoyevsky"

There is no guarantee that the object will actually be of class ArrayList. If you need that guarantee, do this:

ArrayList<String> list = new ArrayList<>(
  new StickyList<>(
    "Scott Fitzgerald", "Fyodor Dostoyevsky"

@Suhaib 2017-06-30 20:44:40

I respect your work. But as someone new to Java, List and rest of its collections (ArrayList, etc...) is already too much to handle. Why you want me to suffer more :-(

@Toothless Seer 2017-06-20 08:33:05

Another Java8 solution (I may have missed the answer among the large set. If so, my apologies). This creates an ArrayList (as opposed to a List) i.e. one can delete elements

package package org.something.util;

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

public class Junk {

    static <T> ArrayList<T>  arrToArrayList(T[] arr){
        return Arrays.asList(arr)

    public static void main(String[] args) {
        String[] sArr = new String[]{"Hello", "cruel", "world"};
        List<String> ret = arrToArrayList(sArr);
        // Verify one can remove an item and print list to verify so

Output is...

@Hemin 2017-05-03 08:38:28

Simplest way to do so is by adding following code. Tried and Tested.

String[] Array1={"one","two","three"};
ArrayList<String> s1= new ArrayList<String>(Arrays.asList(Array1));

@Devendra Lattu 2017-04-07 18:45:50

Even though there are many perfectly written answers to this question, I will add my inputs.

Say you have Element[] array = { new Element(1), new Element(2), new Element(3) };

New ArrayList can be created in the following ways

ArrayList<Element> arraylist_1 = new ArrayList<>(Arrays.asList(array));
ArrayList<Element> arraylist_2 = new ArrayList<>(
    Arrays.asList(new Element[] { new Element(1), new Element(2), new Element(3) }));

// Add through a collection
ArrayList<Element> arraylist_3 = new ArrayList<>();
Collections.addAll(arraylist_3, array);

And they very well support all operations of ArrayList

arraylist_1.add(new Element(4)); // or remove(): Success
arraylist_2.add(new Element(4)); // or remove(): Success
arraylist_3.add(new Element(4)); // or remove(): Success

But the following operations returns just a List view of an ArrayList and not actual ArrayList.

// Returns a List view of array and not actual ArrayList
List<Element> listView_1 = (List<Element>) Arrays.asList(array);
List<Element> listView_2 = Arrays.asList(array);
List<Element> listView_3 = Arrays.asList(new Element(1), new Element(2), new Element(3));

Therefore, they will give error when trying to make some ArrayList operations

listView_1.add(new Element(4)); // Error
listView_2.add(new Element(4)); // Error
listView_3.add(new Element(4)); // Error

More on List representation of array link.

@Bill the Lizard 2008-10-01 14:40:46

new ArrayList<T>(Arrays.asList(myArray));

Make sure that myArray is the same type as T. You'll get a compiler error if you try to create a List<Integer> from an array of int, for example.

@haylem 2012-11-16 17:16:41

(old thread, but just 2 cents as none mention Guava or other libs and some other details)

If You Can, Use Guava

It's worth pointing out the Guava way, which greatly simplifies these shenanigans:


For an Immutable List

Use the ImmutableList class and its of() and copyOf() factory methods (elements can't be null):

List<String> il = ImmutableList.of("string", "elements");  // from varargs
List<String> il = ImmutableList.copyOf(aStringArray);      // from array

For A Mutable List

Use the Lists class and its newArrayList() factory methods:

List<String> l1 = Lists.newArrayList(anotherListOrCollection);    // from collection
List<String> l2 = Lists.newArrayList(aStringArray);               // from array
List<String> l3 = Lists.newArrayList("or", "string", "elements"); // from varargs

Please also note the similar methods for other data structures in other classes, for instance in Sets.

Why Guava?

The main attraction could be to reduce the clutter due to generics for type-safety, as the use of the Guava factory methods allow the types to be inferred most of the time. However, this argument holds less water since Java 7 arrived with the new diamond operator.

But it's not the only reason (and Java 7 isn't everywhere yet): the shorthand syntax is also very handy, and the methods initializers, as seen above, allow to write more expressive code. You do in one Guava call what takes 2 with the current Java Collections.

If You Can't...

For an Immutable List

Use the JDK's Arrays class and its asList() factory method, wrapped with a Collections.unmodifiableList():

List<String> l1 = Collections.unmodifiableList(Arrays.asList(anArrayOfElements));
List<String> l2 = Collections.unmodifiableList(Arrays.asList("element1", "element2"));

Note that the returned type for asList() is a List using a concrete ArrayList implementation, but it is NOT java.util.ArrayList. It's an inner type, which emulates an ArrayList but actually directly references the passed array and makes it "write through" (modifications are reflected in the array).

It forbids modifications through some of the List API's methods by way of simply extending an AbstractList (so, adding or removing elements is unsupported), however it allows calls to set() to override elements. Thus this list isn't truly immutable and a call to asList() should be wrapped with Collections.unmodifiableList().

See the next step if you need a mutable list.

For a Mutable List

Same as above, but wrapped with an actual java.util.ArrayList:

List<String> l1  = new ArrayList<String>(Arrays.asList(array));    // Java 1.5 to 1.6
List<String> l1b = new ArrayList<>(Arrays.asList(array));          // Java 1.7+
List<String> l2  = new ArrayList<String>(Arrays.asList("a", "b")); // Java 1.5 to 1.6
List<String> l2b = new ArrayList<>(Arrays.asList("a", "b"));       // Java 1.7+

For Educational Purposes: The Good ol' Manual Way

// for Java 1.5+
static <T> List<T> arrayToList(final T[] array) {
  final List<T> l = new ArrayList<T>(array.length);

  for (final T s : array) {
  return (l);

// for Java < 1.5 (no generics, no compile-time type-safety, boo!)
static List arrayToList(final Object[] array) {
  final List l = new ArrayList(array.length);

  for (int i = 0; i < array.length; i++) {
  return (l);

@Paul Bellora 2013-01-10 02:54:07

+1 But note that the List returned by Arrays.asList is mutable in that you can still set elements - it just isn't resizable. For immutable lists without Guava you might mention Collections.unmodifiableList.

@afsantos 2013-05-16 13:41:31

@haylem In your section For Educational Purposes: The Good ol' Manual Way, your arrayToList for Java 1.5+ is incorrect. You are instanciating lists of String, and trying to retrieve strings from the given array, instead of using the generic parameter type, T. Other than that, good answer, and +1 for being the only one including the manual way.

@maaartinus 2019-11-19 09:35:27

Id' rename your section "If You Can't... / For an Immutable List" to "For an unmodifiable List" as it can be mutated by later changes to the wrapped array. It's still O(1), but for immutability you have to make a copy, e.g., by Collections.unmodifiableList(new ArrayList<>(Arrays.asList(array))).

@Tim Büthe 2011-06-22 11:30:23

Since this question is pretty old, it surprises me that nobody suggested the simplest form yet:

List<Element> arraylist = Arrays.asList(new Element(1), new Element(2), new Element(3));

As of Java 5, Arrays.asList() takes a varargs parameter and you don't have to construct the array explicitly.

@18446744073709551615 2016-07-12 15:29:21

In particular, List<String> a = Arrays.asList("first","second","third")

@Nicolas Zozol 2014-01-18 13:01:42

If you use :

new ArrayList<T>(Arrays.asList(myArray));

you may create and fill two lists ! Filling twice a big list is exactly what you don't want to do because it will create another Object[] array each time the capacity needs to be extended.

Fortunately the JDK implementation is fast and Arrays.asList(a[]) is very well done. It create a kind of ArrayList named Arrays.ArrayList where the Object[] data points directly to the array.

// in Arrays
public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
//still in Arrays, creating a private unseen class
private static class ArrayList<E>

    private final E[] a;    
    ArrayList(E[] array) {
        a = array; // you point to the previous array

The dangerous side is that if you change the initial array, you change the List ! Are you sure you want that ? Maybe yes, maybe not.

If not, the most understandable way is to do this :

ArrayList<Element> list = new ArrayList<Element>(myArray.length); // you know the initial capacity
for (Element element : myArray) {

Or as said @glglgl, you can create another independant ArrayList with :

new ArrayList<T>(Arrays.asList(myArray));

I love to use Collections, Arrays, or Guava. But if it don't fit, or you don't feel it, just write another inelegant line instead.

@glglgl 2015-02-22 17:03:25

I fail to see the fundamental difference between your loop at the end of the answer and the new ArrayList<T>(Arrays.asList(myArray)); part which you discourage to use. Both do quite the same and have the same complexity.

@Nicolas Zozol 2015-02-22 19:59:37

The Collections one create a pointer at the beginning of the array. My loop create many pointers : one for each array member. So if the original array changes, my poiners are still directed toward the former values.

@glglgl 2015-02-22 21:56:29

new ArrayList<T>(Arrays.asList(myArray)); does the same, it copies the asList to an ArrayList...

@oxy_js 2016-05-02 08:40:44

You can convert using different methods

  1. List<Element> list = Arrays.asList(array);

  2. List<Element> list = new ArrayList();
    Collections.addAll(list, array);

  3. Arraylist list = new Arraylist();

For more detail you can refer to

@Vaseph 2016-02-06 08:56:21

You also can do it with stream in Java 8.

 List<Element> elements =; 

@Raja Anbazhagan 2019-02-04 11:08:23

As of java 8, Collectors.toList() will return an ArrayList. However this may differ in future versions on java.If you want a specific type of collection then use Collectors.toCollection() instead where you can specify which exact type of collection you would want to create. 2015-12-16 20:32:39

Another simple way is to add all elements from the array to a new ArrayList using a for-each loop.

ArrayList<Element> list = new ArrayList<>();

for(Element e : array)

@nekperu15739 2015-05-17 22:07:25

According with the question the answer using java 1.7 is:

ArrayList<Element> arraylist = new ArrayList<Element>(Arrays.<Element>asList(array));

However it's better always use the interface:

List<Element> arraylist = Arrays.<Element>asList(array);

Related Questions

Sponsored Content

26 Answered Questions

95 Answered Questions

[SOLVED] How can I remove a specific item from an array?

  • 2011-04-23 22:17:18
  • Walker
  • 6849598 View
  • 8430 Score
  • 95 Answer
  • Tags:   javascript arrays

47 Answered Questions

[SOLVED] Sort array of objects by string property value

38 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2669356 View
  • 2547 Score
  • 38 Answer
  • Tags:   php arrays unset

11 Answered Questions

[SOLVED] How can I add new array elements at the beginning of an array in Javascript?

  • 2011-11-10 00:35:22
  • Moon
  • 823793 View
  • 1605 Score
  • 11 Answer
  • Tags:   javascript arrays

33 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

31 Answered Questions

[SOLVED] Initialization of an ArrayList in one line

40 Answered Questions

[SOLVED] For-each over an array in JavaScript

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

16 Answered Questions

[SOLVED] Converting 'ArrayList<String> to 'String[]' in Java

Sponsored Content