File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is there a way to convert a ArrayList of Longs to a long array (long[]) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Is there a way to convert a ArrayList of Longs to a long array (long[])" Watch "Is there a way to convert a ArrayList of Longs to a long array (long[])" New topic
Author

Is there a way to convert a ArrayList of Longs to a long array (long[])

Eugenio Flores
Ranch Hand

Joined: Sep 13, 2007
Posts: 38
Hello, I wouldn't like to iterate through a List of Longs to get the corresponding primitive array.

Is there a JSE API to get the a primitive array?

I'm asking here because in my project, my search method returns a long[]

Thanks in advance.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

I would also like to know if there is a clean way to do that... Right now I'm using very basic way of iterating through the list and adding elements to the array...


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

How about this piece of code:



Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

I'm not sure there is a way to return a primitive array, I know that as of 1.5 List has a method to returning an array using generics, so something like this:


but only works with Objects and not primitives.

You may just have to loop through and add to an array.

Sean


I love this place!
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Just now I read carefully the title of this thread... so Sean's code is correct, my code does the opposite of what's being asked.
Eugenio Flores
Ranch Hand

Joined: Sep 13, 2007
Posts: 38
Thanks for the answer, I believe I would have to iterate...

Thanks again.

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

I think everybody wants such a method for the implementation of the find-method I didn't find one (and I'm pretty sure it does not exist in the JDK), so a simple iteration will do.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a way to convert a ArrayList of Longs to a long array (long[])