File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Object array to Primitive Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object array to Primitive Array" Watch "Object array to Primitive Array" New topic

Object array to Primitive Array

vidya Sreedharan

Joined: Jun 27, 2007
Posts: 4

Is there a better way of copying elements from a Long arrray to a primitive long array rather than assigning each element in a for loop.

marc weber

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

Unless I'm mistaken, I think a loop is your only option.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

I concur.

Even if there is a method, behind the scened, it's probably just doing a loop as well. Write your own loop, and make it as efficient as you can.

Good luck!

-Cameron McKenzie
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

There is no convenient way to do this with a Java language construct, nor is there a convenient method in the standard Java API to do this. So you will have to write a loop.

However, the Apache Jakarta Commons Lang utility library (open source) contains a class ArrayUtils which makes it easy:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
vidya Sreedharan

Joined: Jun 27, 2007
Posts: 4
Thanks everyone!!
I agree. Here's the link:
subject: Object array to Primitive Array
It's not a secret anymore!