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
If we see the definition of Arrays.asList()
method you will get something like this:
public static List asList(T... a) //varargs are of T type.
So, you might initialize arraylist
like this:
List arraylist = Arrays.asList(new Element(1), new Element(2), new Element(3));
Note : each
new Element(int args)
will be treated as Individual Object and can be passed as avar-args
.
There might be another answer for this question too.
If you see declaration for java.util.Collections.addAll()
method you will get something like this:
public static boolean addAll(Collection super T> c, T... a);
So, this code is also useful to do so
Collections.addAll(arraylist, array);