File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. Collection" Watch "Reg. Collection" New topic

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.
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

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.

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: Reg. Collection
It's not a secret anymore!