views:

105

answers:

2

Hello, everyone!

I need to do lots of conversions between primitivetype[] and boxedtype[] (both directions).
Such as: Integer[] <-> int[], Double[] <-> double[], ...

I wanted to know, if there's some quasi-standards APIs out there, which provide such functionality, before I write such utility methods by myself.

Java has 8 primitive types, so it would be quite a (copy-paste) work...

Thank you.

+3  A: 

ArrayUtils

ArrayUtils.toObject( primitive[] )

and

ArrayUtil.toPrimitive( wrapper[] )

Matthew Flaschen
Ah, so it's Apache Commons Lang! I've looked only in Apache Commons Collections.
java.is.for.desktop
then I guess upvote )
Bozho
Yeah, Apache's layers of project hierarchy remind me of a Matryoshka doll. It's can be quite hard to find things.
Matthew Flaschen
As a Russian, I may correct you: It's called Matryoshka ;) http://en.wikipedia.org/wiki/Matryoshka_doll
java.is.for.desktop
"Matryoshka" is more common I think ;)
Bozho
In spanish they are called ( well I do ) "Matrushka" .. :P cultural note.
OscarRyz
A: 

Lately I've written a LGPL3 library, so it isn't stardard nor widely adopted, that try to addressing these problems:

Integer[] boxed = ... ;
int[] primitive = $(boxed).toIntArray();

and viceversa:

boxed = $(boxed).toArray();

But I'm hoping that you will appreciate some extra features like casting:

byte[] bytes = ...;
int[] ints = $(bytes).toIntArray();
short[] shorts = $(bytes).toShortArray();
dfa
The idea of your API is good!
java.is.for.desktop
Not a fan of the jQuery-style do-everything global function.
Matthew Flaschen