my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. Collection" Watch "Reg. Collection" New topic
Author

Reg. Collection

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
How does the following method of Collection interface work ?
Object[] toArray( Object[] o );
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Returns an array containing all of the elements in this collection whose runtime type 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.
Ragu
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
I read the help from API, but am not still clear.
Can someone give an example.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Consider this:

the argument just specifies of which type the returned array should be. Then we just cast the result into what we want. ArrayStoreException may get thrown if the type you specify (new String[0]) is not compatible with the object in the collection.
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg. Collection