This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections" Watch "Collections" New topic
Author

Collections

Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,

Could you provide the code which explains

Object[] toArray(Object a[])

I have tried,but i didn't get it.

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Ah, yes... In Collections.

I missed that one. Thanks, Henry!
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
You can find more information on its javadoc


<T> T[] toArray(T[] a)

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.


Naseem
[ August 10, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi Naseem,


It's Clear


Thank You.
 
 
subject: Collections