Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Collections  RSS feed

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could you provide the code which explains

Object[] toArray(Object a[])

I have tried,but i didn't get it.

Thanks
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get it either. This appears to be a method that takes an Object array and returns an Object array. There must be something missing here (since I'm fairly certain you don't just want "return a").
 
author
Sheriff
Posts: 23569
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard toArray() method (of a collection) returns all the objects in the collection in an Object array. It is an Object array, even if all of the objects in the collection are strings.

While you can cast the objects in the array to String, you can't cast the Object array to an String array. This can get pretty inconvenient sometimes.


IMHO, the correct solution would have been to return an Object array that can be casted to a common base class array. But they didn't do that. Instead, they provided an overloaded toArray() method that takes an Object array.

This version of toArray() will take an Object array, but that Object array may be a String or any subtype of the Object array. It will then return an Object array which may be casted to the original type of array which was originally passed to the method.... So if you know all the items in the collection are Strings, you can pass it a String array, and cast the returned Object array to a String array.

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes... In Collections.

I missed that one. Thanks, Henry!
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find more information on its javadoc


<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this collection; the runtime type of the returned array 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.
If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.





Here toArray() method will populate strResult array with the Collection element.


Naseem
[ August 10, 2006: Message edited by: Naseem Khan ]
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naseem,


It's Clear


Thank You.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!