How to Convert Array to ArrayList in Java?

This article analyzes answers for a top-voted questions on Stack Overflow. The person who asked this question got a lot of reputation points, which could grant him permissions to do a lot of things on Stack Overflow. This does not make sense to me, but let’s take a look at the question first.

The question is “how to convert the following array to an ArrayList?”.

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

1. Most popular and accepted answer

The most popular and the accepted answer is the following:

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

First, let’s take a look at the Java Doc for the constructor method of ArrayList.

ArrayList(Collection < ? extends E > c) : Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

So what the constructor does is the following:
1. Convert the collection c to an array
2. Copy the array to ArrayList’s own back array called “elementData”

Here is the source code of Contructor of ArrayList.

public ArrayList(Collection<? extends E> c) {
       elementData = c.toArray();
       size = elementData.length;
 
       if (elementData.getClass() != Object[].class)
             elementData = Arrays.copyOf(elementData, size, Object[].class);
}

2. Next popular answer

The next popular answer is:

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

It is not the best, because the size of the list returned from asList() is fixed. Actually the list returned is not java.util.ArrayList, but a private static class defined inside java.util.Arrays. We know ArrayList is essentially implemented as an array, and the list returned from asList() is a fixed-size list backed by the original array. In this way, if add or remove elements from the returned list, an UnsupportedOperationException will be thrown.

list.add(new Element(4));
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
	at collection.ConvertArray.main(ConvertArray.java:22)

3. Another Solution

This solution is from Otto’s comment below.

Element[] array = {new Element(1), new Element(2)};
List<element> list = new ArrayList<element>(array.length);
Collections.addAll(list, array);

4. Indications of the question

The problem is not hard, but interesting. Every Java programmer knows ArrayList, but it’s easy to make such a mistake. I guess that is why this question is so popular. If a similar question asked about a Java library in a specific domain, it would be less likely to become so popular.

There are several answers that provide the same solution. This is also true for a lot of other questions on Stack Overflow, I guess people just don’t care what others say if they would like to answer a question!

Reference: The stackoverflow question link

13 thoughts on “How to Convert Array to ArrayList in Java?”

  1. will this conversion
    ArrayList arrayList = new ArrayList(Arrays.asList(array));

    of array to arraylist change sequence or not

  2. i have one question.how can link array names with string names like i will declare an array a1 and if any where i compare a string “a1” then it should link with array a1 and i should be able to manupulate array a1 values using this string a1. is it possible to do like that

  3. I take back what I said about Hibernate not being able to handle Arrays$ArrayList. I was getting an exception before, but now it is working. Maybe it won’t work if there’s autoboxing involved.

  4. Thanks for adding the solution to the post.
    I’d also like to add that solution 2 has the best performance, but does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList and that might fail depending on the expected implementation. Hibernate/JPA’s Query.setParameter(), for example, can’t handle that List type.

  5. Hi. The most popular answer works, but it is not efficient. From what I have tested, this solution offers a better performance:
    Element[] array = {new Element(1), new Element(2)};
    List list = new ArrayList(array.length);
    Collections.addAll(list, array);

  6. Thanks for comment. I don’t expect any other way. This post only shows how it works now.

  7. I looked over your post again and saw that I misunderstood you. Yes, the new ArrayList is sized to exactly fit the original array, so if someone adds an additional element, the ArrayList is resized. Seems reasonable. How else would you expect it to work?

  8. Came here from the DZone posting. What version of Java are you basing this on? I took a look at the source for Array(Collection) and Arrays.asList(T…) in Java 6 and 7 and it looks like they use very direct, efficient approaches:

    public ArrayList(Collection c) {
    elementData = c.toArray();
    size = elementData.length;
    // c.toArray might (incorrectly) not return Object[] (see 6260652)
    if (elementData.getClass() != Object[].class)
    elementData = Arrays.copyOf(elementData, size, Object[].class);
    }

    In Arrays:
    public static List asList(T… a) {
    return new ArrayList(a);
    }

    ArrayList(E[] array) {
    if (array==null)
    throw new NullPointerException();
    a = array;
    }

Leave a Comment