up vote 38 down vote favorite
7

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.

flag

4  
+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

4 Answers

up vote 57 down vote accepted
new ArrayList<Element>(Arrays.asList(array))
link|flag
Duh. Thanks Tom I knew I had seen it someplace (I even had the API docs open on the Arrays class, but was blind to the first method). – Ron Tuffin Oct 1 '08 at 14:41
4  
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
up vote 6 down vote
new ArrayList<T>(Arrays.asList(myArray));
link|flag
2  
you need to type faster ;) – Tom Oct 1 '08 at 14:41
1  
I was just thinking the same thing! :) – Bill the Lizard Oct 1 '08 at 14:42
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
1  
and again you should type faster :) – Tom Oct 1 '08 at 14:46
show 1 more comment
up vote 6 down vote

The simplest answer is to do:

Element[] array = new Element[] { new Element(1),new Element(2),new Element(3) };
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.
link|flag
up vote 3 down vote

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

List<Element> arraylist = Arrays.asList(array);
link|flag
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
2  
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
2  
@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

Your Answer

get an OpenID
or
never shown

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