my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Convert collection to primitive array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert collection to primitive array" Watch "Convert collection to primitive array" New topic
Author

Convert collection to primitive array

Niclas Åstrand
Greenhorn

Joined: Oct 15, 2009
Posts: 9
Hi all!

Is there a way, other than writing your own method, for converting a collection of say Long (List<Long>) to an array of long?

Niclas
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5449
    
  13

Hi Niclas,

This is more a Java in General or Beginning Java question. I know it is for the SCJD, because I had exactly the same question as you and you need it I guess for the find-method I googled around, found nothing appropriate and just wrote 4 lines of really impressive code to get the job done

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

So let's slide this over to the Beginning Java forum!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Niclas Åstrand
Greenhorn

Joined: Oct 15, 2009
Posts: 9
Sorry for posting in the wrong forum.

Thanks Roel.

Niclas
senthil kumar. J
Greenhorn

Joined: May 04, 2010
Posts: 5
Why don't you use toArrary() method provided by the List and pass list to an array.

Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
senthil kumar. J wrote:Why don't you use toArrary() method provided by the List and pass list to an array.


Please note that this does not compile:

The Arrays.asList call is OK, the primitive ints will be autoboxed into Integer.
However, the signature of toArray is:
<T> T[] toArray(T[] a)

Here T must stand for a reference type, not a primitive.

Unfortunately, T is not related to E (Java does not allow type parameters to be bounded using super, so <T super E> T[] toArray(T[] a) is illegal), which means you can compile the following (obviously invalid) code:


Niclas Åstrand
Greenhorn

Joined: Oct 15, 2009
Posts: 9
Thank you for your excellent clarification Istvan!
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Niclas Åstrand wrote:Is there a way, other than writing your own method, for converting a collection of say Long (List<Long>) to an array of long?

There's no other way in the standard classes included in the JDK, as far as I know. Of course third-party libraries may do it for you. The only one I know that has this is Guava - see Ints.toArray() for example. Wouldn't surprise me if there are others. Then again, this is a really simple method to write for yourself, so it's not worth putting a lot of effort into searching for other implementations.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5449
    
  13

Mike Simmons wrote:There's no other way in the standard classes included in the JDK, as far as I know. Of course third-party libraries may do it for you.

He asks this question with regard to SCJD certification, so use of 3rd party libraries are not allowed, only the JDK. So he will to code this on his own If Guava is open-source you can get the sources and have a look at the Ints.toArray() method for a bit of inspiration
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert collection to primitive array