Convert an ArrayList to an object array
You can use this code
ArrayList<TypeA> a = new ArrayList<TypeA>();
Object[] o = a.toArray();
Then if you want that to get that object back into TypeA just check it with instanceOf method.
Something like the standard Collection.toArray(T[]) should do what you need (note that ArrayList
implements Collection
):
TypeA[] array = a.toArray(new TypeA[a.size()]);
On a side note, you should consider defining a
to be of type List<TypeA>
rather than ArrayList<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 of 0
as the size of the array passed to a.toArray(TypeA[])