I have an array that is initialised 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 = ???;

I am sure I have done this before, but the solution is sitting just at the edge of my memory.

share|improve this question

63  
+1 for being the first hit on google when searching for "java construct arraylist from array". Thanks! – Greg Hewgill Oct 8 '09 at 22:45
feedback

6 Answers

up vote 389 down vote accepted
new ArrayList<Element>(Arrays.asList(array))
share|improve this answer
26  
Yep. And in the (most common) case where you just want a list, the new ArrayList call is unecessary as well. – Calum Oct 1 '08 at 14:41
   
how do you access the new arraylist if there is no name for it? – Luron Oct 3 '10 at 23:53
7  
@Luron - just use List<ClassName> list = Arrays.asList(array) – Pool Jun 29 '11 at 15:18
16  
@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. – Code Jockey Sep 26 '11 at 19:04
feedback

Given:

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

The simplest answer is to do:

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

This will work fine. But some caveats:

  1. The list returned from asList has fixed size. So, if you want to be able to add or remove elements from the returned list in your code, you'll need to wrap it in a new ArrayList(). Otherwise you'll get an UnsupportedOperationException.
  2. The list returned from asList is backed by the original array. If you modify the original array, the list will be modified as well. This may be surprising.
share|improve this answer
4  
+1 for the UnsupportedOperationException caveat. – Senthil Kumar May 1 at 10:17
What is the time complexity of both operations? I mean with and without using explicit arraylist construction. – damned May 23 at 2:17
Arrays.asList() merely creates an ArrayList by wrapping the existing array so it is O(1). – Alex Miller May 23 at 13:15
Wrapping in a new ArrayList() will cause all elements of the fixed size list to be iterated and added to the new ArrayList so is O(n). – Alex Miller May 23 at 13:21
feedback
new ArrayList<T>(Arrays.asList(myArray));
share|improve this answer
11  
you need to type faster ;) – Tom Oct 1 '08 at 14:41
5  
I was just thinking the same thing! :) – Bill the Lizard Oct 1 '08 at 14:42
1  
This one is actually more correct (Tom's is missing the type annotation) :) – Calum Oct 1 '08 at 14:42
you're right, I updated my answer – Tom Oct 1 '08 at 14:44
6  
and again you should type faster :) – Tom Oct 1 '08 at 14:46
show 1 more comment
feedback

Since this question is pretty old, it suprises 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 explicit.

share|improve this answer
1  
Thanks Tim; good point. I have up-voted this but will retain the other accepted answer as the question was specifically about the conversion of an array to a List. – Ron Tuffin Jun 22 '11 at 13:47
3  
Fair enough. Maybe that helps someone else. – Tim Büthe Jun 27 '11 at 8:00
feedback

You probably just need a List, not an ArrayList. In that case you can just do:

List<Element> arraylist = Arrays.asList(array);
share|improve this answer
That will be backed by the original input array, which is why you (probably) want to wrap it in a new ArrayList. – Bill the Lizard Oct 1 '08 at 14:46
5  
Be careful with this solution. If you look, Arrays ISN'T returning a true java.util.ArrayList. It's returning an inner class that implements the required methods, but you cannot change the memebers in the list. It's merely a wrapper around an array. – Mikezx6r Oct 1 '08 at 14:47
You can cast the List<Element> item to an ArrayList<Element> – monksy Oct 9 '09 at 22:48
5  
@Mikezx6r: little correction: it's a fixed-size list. You can change the elements of the list (set method), you cannot change the size of the list (not add or remove elements)! – Carlos Heuberger Dec 4 '09 at 13:10
feedback

Another way (although essentially equivalent to the new ArrayList(Arrays.asList(array)) solution performance-wise:

Collections.addAll(arraylist, array);
share|improve this answer
feedback

protected by Robert Harvey May 11 '11 at 3:26

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.