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 = ???;
|
|
|||||||||||||||||||||
|
Given:
The simplest answer is to do:
This will work fine. But some caveats:
|
|||||||||||||||||
|
Since this question is pretty old, it suprises me that nobody suggested the simplest form yet:
As of Java 5, Arrays.asList() takes a varargs parameter and you don't have to construct the array explicit. |
|||||
|
(old thread, but just 2 cents as none mention Guava or other libs and some other details) If You Can, Use GuavaIt's worth pointing out the Guava way, which greatly simplifies these shenanigans: UsageFor an Immutable ListUse the
For A Mutable ListUse the
Please also note the similar methods for other data structures in other classes, for instance in 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 ListUse the JDK's
Note that the returned type for It forbids modifications through some of the See the next step if you need a mutable list. For a Mutable ListSame as above, but wrapped with an actual
For Educational Purposes: The Good ol' Manual Way
|
|||||||||||||||||
|
|
|||
|
Another way (although essentially equivalent to the
|
||||
|
You probably just need a List, not an ArrayList. In that case you can just do:
|
|||||||||||||||||||||
|
To convert an array to an ArrayList, developers often do this:
The constructor of ArrayList can accept a Collection type, which is also a super type for |
|||||
|
Another update, almost ending year 2014, you can do it with Java 8 too:
A few characters would be saved, if this could be just a
|
|||||
|
If you use :
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
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 :
Don't use Collections, Arrays, or Guava. I love to use it, but if it don't fit, don't use it. Write another inelegant line instead. Well... still use and learn Guava if you can. |
|||||||||||||
|
|
|||
|
According with the question the answer using java 1.7 is:
However it's better always use the interface:
|
|||
|
There is another option if your goal is to generate a fixed list at runtime, which is as simple as it is effective:
|
|||||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?