aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q about Collection: toArray operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q about Collection: toArray operation" Watch "Q about Collection: toArray operation" New topic
Author

Q about Collection: toArray operation

Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 975
Dear all,
The Collection interface provides the signature of
the method toArray.
In the Sun's tutorial on collections it says:
The following snippet dumps the contents of c into a newly
allocated array of String whose length is identical to the
number of elements in c:
String[] a = (String[]) c.toArray(new String[0]);

Why does it give the array argument a length of 0? And not the
amount of elements in c?
Best regards,
Gian Franco Casula
[ February 20, 2004: Message edited by: Gian Franco Casula ]
[ February 20, 2004: Message edited by: Gian Franco Casula ]
[ February 20, 2004: Message edited by: Gian Franco Casula ]

"Eppur si muove!"
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12683
    
    5
In the case of the empty array, toArray will use the type to create a new array. However, if you know the size, you can pass in an array of exactly the right size and it will be used. I suppose they figure it is more flexible to provide both ways of operating.
Bill


Java Resources at www.wbrogden.com
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 975
Thank you Bill,
Gian Franco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q about Collection: toArray operation
 
Similar Threads
A String Question
Opinion Kushner: afore ya go
Comparision between various frameworks
Order of evaluation of equality operator
Q about IBM 486 and others: which books are reccomended?