• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting a List into an Array?

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is something that has long irritated me, but I've never been able to find a solution. If I have a List of apples and I want to turn it into an array of apples, I have to do this:



I'd love to be able to do this:



Unfortunately, this doesn't work because the toArray method returns an array of Objects, not an array of Apples. So is there any better way to do this? Having to write that silly for loop any time I want to make this happen seems so wasteful.

Thanks.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#toArray%28T[]%29
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were close, you have to scroll down one more method to find the one you're looking for http://java.sun.com/javase/6/docs/api/java/util/List.html#toArray(T[])

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be able to use the no-arguments toArray method and cast the resultant array. But remember casts are hazardous.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You may be able to use the no-arguments toArray method and cast the resultant array.


No you may not. toArray() returns an Object[] - both in reference type and in actual type. Casting this to anything else, like Apple[], will throw a ClassCastException.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a very good topic. You guys answered well because I equally had same experience some times back.
But, am yet really fish out what List<T>.toArray() does.
In what situation can it be used?
Maybe code sample will clearify that.
Help?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collection.toArray() does something that looks like this (actual implementation usually is different but the results are the same):
Collection.toArray(T[]) on the other hand does something like this (again, actual implementation may vary):
The most important step there is line 5 - instead of creating a new Object[] it uses reflection to create an array of the proper type.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Rob; I was obviously mistaken.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies, folks. I had seen that method there, but I guess I never paid any attention to what it did. As an addendum to Rob's reply, here's the actual implementation of the method ArrayList.toArray(T[] t):

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic