• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Eugenio Flores
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this piece of code:

 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer, I believe I would have to iterate...

Thanks again.

 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic