Is there a command in java for conversion of an ArrayList into a object array. I know how to do this copying each object from the arrayList into the object array, but I was wondering if would it be done automatically.
I want something like this:
ArrayList<TypeA> a;
// Let's imagine "a" was filled with TypeA objects
TypeA[] array = MagicalCommand(a);
Something like the standard Collection.toArray(T[]) should do what you need (note that
ArrayListimplementsCollection):On a side note, you should consider defining
ato be of typeList<TypeA>rather thanArrayList<TypeA>, this avoid some implementation specific definition that may not really be applicable for your application.Also, please see this question about the use of
a.size()instead of0as the size of the array passed toa.toArray(TypeA[])