This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
I don't get it either. This appears to be a method that takes an Object array and returns an Object array. There must be something missing here (since I'm fairly certain you don't just want "return a").
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
The standard toArray() method (of a collection) returns all the objects in the collection in an Object array. It is an Object array, even if all of the objects in the collection are strings.
While you can cast the objects in the array to String, you can't cast the Object array to an String array. This can get pretty inconvenient sometimes.
IMHO, the correct solution would have been to return an Object array that can be casted to a common base class array. But they didn't do that. Instead, they provided an overloaded toArray() method that takes an Object array.
This version of toArray() will take an Object array, but that Object array may be a String or any subtype of the Object array. It will then return an Object array which may be casted to the original type of array which was originally passed to the method.... So if you know all the items in the collection are Strings, you can pass it a String array, and cast the returned Object array to a String array.
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection. If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.
Here toArray() method will populate strResult array with the Collection element.