How to Convert Array to ArrayList in Java

DZone 's Guide to

How to Convert Array to ArrayList in Java

Check out this short post to learn how to convert an Array to an ArrayList in Java.

· Java Zone ·
Free Resource

Man sitting at three computer monitors

Check out this short post to learn how to convert an Array to an ArrayList in Java.

This is a question that I wanted to take a look at for myself because it is one of the top viewed and most voted questions on StackOverflow.

The question is about how to convert the following Array to an ArrayList.

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

1. The Most Popular and Accepted Answer

The most popular and 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 c): Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

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

If the add()  method is invoked now, the elementData array will be copied to a new larger array. As for the code below, the size grows 1.5 times the old size:

public void ensureCapacity(int minCapacity) {
      int oldCapacity = elementData.length;
      if (minCapacity > oldCapacity) {
          Object oldData[] = elementData;
          int newCapacity = (oldCapacity * 3)/2 + 1;
          if (newCapacity < minCapacity)
              newCapacity = minCapacity;
             // minCapacity is usually close to size, so this is a win:
             elementData = Arrays.copyOf(elementData, newCapacity);

2. Second Most Popular Answer

The second most 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. 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));

An exception in the thread “main”  java.lang.ClassCastException: java.util.Arrays$ArrayList  cannot be cast to  java.util.ArrayList  at  collection.ConvertArray.main(ConvertArray.java:22) . 

3. Indications of the Question

Every Java programmer knows ArrayList; it is simple yet easy to make mistakes. I guess that is why this question is so popular. If you were to ask a similar question about a Java library in a specific domain, it would be less likely to become so popular.

There are several answers that basically indicate the same solution. This is true for a lot of questions — I guess people don’t care, they like talking!

If you wish to view this question and its solutions entirely, check out StackOverflow.

If you enjoyed this article and want to learn more about Java Collections, check out this collection of tutorials and articles on all things Java Collections.

array, arraylist, collections, java, tips and tricks

Published at DZone with permission of Ryan Wang . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}