Would my executable statement look like this?:
MyArray [] = MyList.toArray(); No. Two reasons: 1. What the heck is
MyArray []? That will never compile. 2. Notice that the toArray method comes in more than one flavor.
Let's say I had a class name Person, and I'd created a List of Person objects named personList. To get a Person[] from this personList, I'd do the following.
Person[] people = new Person[personList.size()];
people = (Person[])personList.toArray(people);
The assignment statement in the second statement isn't necessary. I could have simply used the following, instead.
Person[] people = new Person[personList.size()];
personList.toArray(people);
Or, the same thing could be accomplished in a single line, with an extra (empty array) object created.
Person[] people = (Person[])personList.toArray(new Person[0]);
These are all three different variations to accomplish the same thing - creating a Person[] from the personList. I recommend you pick whichever style is clearest for you. Of course, for any of these to work, every component in the personList must be an object of type Person.
For a decent explanation of just what this toArray method is doing, take a look at
the documentation.
[ May 03, 2004: Message edited by: Dirk Schreckmann ]