Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Implement Collection toArray
|
Posted: Jun 17, 2003 12:49 AM
|
|
From looking at the documentation of toArray(object[]) in the Collection interface, you will want to return an array of Objects which match the type of the array passed in. That means if you have a heterogeneous collection of objects, only those which match that type should be returned in the array. You will probably use instanceof to determine this. If you want to follow the spec closely, you need to have a look at the documentation regarding whether to use the array passed in or create a new one (it depends on whether it can accommodate all the objects you want to return). You might first count all the objects in your collection that are an instance of the element type of the array passed in, then either build a new array of that size, or use the one passed in. Then you could go through your collection and insert those items into the array (if this two-pass method is too slow for your needs, then the next step will be to do the performance tuning).
|
|