Alex Rivera | Logout

Convert an ArrayList to an object array

Asked 2010-04-30T14:47:48.113
46

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);
Edit
Report

2 Answers

12

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.

answered 2010-05-02T10:59:38.887
9
answered 2010-04-30T14:50:29.763

Your Answer