This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Q about Collection: toArray operation

 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bill,
Gian Franco
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!