How to create an ArrayList from array?
Element[] array = {new Element(1), new Element(2), new Element(3)};
How do I convert the above variable of type Element[]
into a variable of type ArrayList<Element>
?
ArrayList<Element> arrayList = ...;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Notifications
You can use the following instruction:
If You Can, Use Guava
It’s worth pointing out the Guava way, which greatly simplifies these shenanigans:
Usage
For an Immutable List
Use the
ImmutableList
class and itsof()
andcopyOf()
factory methods (elements can’t be null):For A Mutable List
Use the
Lists
class and itsnewArrayList()
factory methods:Please also note the similar methods for other data structures in other classes, for instance in
Sets
.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 List
Use the JDK’s
Arrays
class and itsasList()
factory method, wrapped with aCollections.unmodifiableList()
:Note that the returned type for
asList()
is aList
using a concreteArrayList
implementation, but it is NOTjava.util.ArrayList
. It’s an inner type, which emulates anArrayList
but actually directly references the passed array and makes it “write through” (modifications are reflected in the array).It forbids modifications through some of the
List
API’s methods by way of simply extending anAbstractList
(so, adding or removing elements is unsupported), however it allows calls toset()
to override elements. Thus this list isn’t truly immutable and a call toasList()
should be wrapped withCollections.unmodifiableList()
.See the next step if you need a mutable list.
For a Mutable List
Same as above, but wrapped with an actual
java.util.ArrayList
:For Educational Purposes: The Good ol’ Manual Way
Java 7+
In Java 1.7 or later, the standard way to do this (generate a basic non-cryptographically secure random integer in the range [min, max]) is as follows:
See the relevant JavaDoc. This approach has the advantage of not needing to explicitly initialize a java.util.Random instance, which can be a source of confusion and error if used inappropriately.
However, conversely with ThreadLocalRandom there is no way to explicitly set the seed so it can be difficult to reproduce results in situations where that is useful such as testing or saving game states or similar.
Java 17+
As of Java 17, the psuedorandom number generating classes in the standard library implement the
RandomGenerator
interface. See the linked JavaDoc for more information. For example, if a cryptographically strong random number generator is desired, theSecureRandom
class can be used.Earlier Java
Before Java 1.7, the standard way to do this is as follows:
See the relevant JavaDoc. In practice, the java.util.Random class is often preferable to java.lang.Math.random().
In particular, there is no need to reinvent the random integer generation wheel when there is a straightforward API within the standard library to accomplish the task.