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 = ???;
Demographics. Technologies. Salaries. Career satisfaction.
I have an array that is initialized like:
I would like to convert this array into an object of the ArrayList class.
|
||||
|
|
|||||||||||||||||||||
|
Given:
The simplest answer is to do:
This will work fine. But some caveats:
|
|||||||||||||||||||||
|
(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
|
|||||||||||||
|
Since this question is pretty old, it surprises me that nobody suggested the simplest form yet:
As of Java 5, |
|||||
|
Make sure that |
||||
|
Another way (although essentially equivalent to the
|
||||
|
You probably just need a List, not an ArrayList. In that case you can just do:
|
|||||||||||||||||||||
|
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
|
|||||||||||||
|
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 |
|||||||||
|
Java 9In Java 9, you can use
This would return an immutable list containing three elements. If you want a mutable list, pass that list to the
JEP 269: Convenience Factory Methods for CollectionsJEP 269 provides some convenience factory methods for Collections API. This factory methods are not in current Java version, which is 8, but are planned for Java 9 release. You can try out this feature using JDK 9 Early Access. |
||||
|
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 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 :
Or as said @glglgl, you can create another independant ArrayList with :
I love to use |
|||||||||||||
|
According with the question the answer using java 1.7 is:
However it's better always use the interface:
|
|||
|
|
|||
|
You can convert using different methods
For more detail you can refer to http://javarevisited.blogspot.in/2011/06/converting-array-to-arraylist-in-java.html |
||||
|
You also can do it with stream in Java 8.
|
|||
|
|
||||
|
Another simple way is to add all elements from the array to a new ArrayList using a for-each loop.
|
|||
|
Since Java 8 there is easier way to transform:
|
|||
|
and the common newest way to create array is observableArrays thus the answer is
that is according to Oracle Docs
|
|||
|
Arrays.asList(yourArray) method is used to convert your array into arrayList
|
|||
|
Quick way to Convert Array to ArrayList is to use Array.asList() Method |
||||
|
If your
To learn more about |
||||
|
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 or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?