This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes 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
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

toArray(Object [] a) ?

 
pioneer
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could anybody explain what's the parameter in the function use for? I'm not quite understand the function specification.
As the example in the API:


Suppose v is a List known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
String[] x = (String[]) v.toArray(new String[0]);


why 'new String[0]' is used?
 
pioneer
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This tells the method to allocate a String[] array rather than an Object[] array.
If it allocated an Object[] array, you could not legally cast the array to String[], even though all of its elements were Strings.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if the passed array is large enough to contain the values to be returned, the method returns the passed array reference (that now contains the values to be returned) and not a newly created one. The source code of the Vector class shows that:
 
Yan Bai
pioneer
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ron and Valentin, I am still not quite understand under what circumstances is this function used
could you please give me a simple example showing the difference between the simple toArray() and the one has a parameter. Thanks
[ August 27, 2002: Message edited by: Yan Bai ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toArray() (without parameter) will always return an array of type Object[] that you won't be able to cast. toArray(new String[0]) will return an array of type Object[] that you may cast to String[].
For instance,

line 1 will throw a ClassCastException at runtime because you are not allowed to cast Object[] (the return type of toArray()) to String[].
Lines 2 and 3 compile and run successfully.
 
Yan Bai
pioneer
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Valentin, This is quite clear.
 
pioneer
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Val and Ron:
What is the significance of 0 (zero) in following
statement
...(new String[0])
Thanks
Barkat
 
Ron Newman
pioneer
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The [0] just means you're allocating an array of length 0 -- an empty array. You could use any value instead of 0 and it would still work.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, if instead of 0 you give a size that is big enough to contain all elements of the collection, the latter will fill up the array passed in argument instead of creating a new one (think performance!)
 
Hot dog! An advertiser loves us THIS much:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!