File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Converting a List into an Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Converting a List into an Array?" Watch "Converting a List into an Array?" New topic
Author

Converting a List into an Array?

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
try http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#toArray%28T[]%29
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
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[])



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32701
    
    4
You may be able to use the no-arguments toArray method and cast the resultant array. But remember casts are hazardous.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Paul Okeke
Ranch Hand

Joined: May 16, 2009
Posts: 58
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?


OCJP 1.6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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
Sheriff

Joined: Oct 13, 2005
Posts: 32701
    
    4
Thank you, Rob; I was obviously mistaken.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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):

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Converting a List into an Array?
 
Similar Threads
Generics - type argument inference
Generics problem......
k&b: generics ? syntax
Generics <?>
Generic class declarations