So basically the method signature you want is like this:
public static <E> E[] toArray(List<E> list);
And usage would be:
List<String> strList = new ArrayList<String>();
String[] strArr = toArray(strList);
List<Integer> intList = new ArrayList<Integer>();
Integer[] intArr = toArray(intList);
Now, as mentioned, List
already defines a method that does this except it's not really generic. You have to give it an array to copy in to or use Object[]
.
List<String> strList = new ArrayList<String>();
Object[] objArr = strList.toArray();
String[] strArr = strList.toArray(new String[strList.size()]);
I assume neither of those are what you are wanting. Unfortunately, there is not a way to do exactly what you are asking because you cannot create an array generically in Java. Going back to the method:
public static <E> E[] toArray(List<E> list) {
E[] arr = new E[list.size()]; // doesn't compile
return list.toArray(arr);
}
The problem being that E
is erased so that is not compilable Java code. <E>
doesn't exist at run time to be able to do new E
.
So the closest you can get is like this:
public static <E> E[] toArray(List<E> list, Class<E> e) {
@SuppressWarnings("unchecked")
final E[] arr = (E[])Array.newInstance(e, list.size());
return list.toArray(arr);
}
But that is not really any more convenient to use:
List<String> strList = new ArrayList<String>();
String[] strArr = toArray(strList, String.class);
There is another interesting approach which is to extend the ArrayList
and add this functionality to it. But you still need the Class<E>
object. You'd do it like this:
import java.util.Arraylist;
import java.util.Objects;
import java.lang.reflect.Array;
public class TypedList<E> extends ArrayList<E> {
private final Class<E> e;
public TypedList(Class<E> e) {
this.e = Objects.requireNonNull(e);
}
@Override
public E[] toArray() {
@SuppressWarnings("unchecked")
final E[] arr = (E[])Array.newInstance(e, size());
return toArray(arr);
}
}
This lets you specify the Class<E>
at the very beginning and later you can call toArray
with no arguments, e.g.:
TypedList<String> strList = new TypedList<String>(String.class);
String[] strArray = strList.toArray();
What I'd recommend is to steer away from trying to use arrays generically. They are not good for this type of thing.