Use ArrayUtils.toObject()
and ArrayUtils.toPrimitive()
to translate between primitive arrays and
object arrays. The following example demonstrates the translation
from a primitive array to an object array and vice versa:
import org.apache.commons.lang.ArrayUtils; long[] primitiveArray = new long[] { 12, 100, 2929, 3323 }; Long[] objectArray = ArrayUtils.toObject( primitiveArray ); Double[] doubleObjects = new Double[] { new Double( 3.22, 5.222, 3.221 ) }; double[] doublePrimitives = ArrayUtils.toPrimitive( doubleObject );
The result from both translations is an array of equal length and
equal contents. The first translation takes a
long[]
and translates the array to a
Long[]
, and the second translation takes a
Double[]
and turns it into a
double[]
.
Assume that the following example uses an external library that
expects a list of Double
objects. The existing
system uses an array of double primitives, and you need to
“step up” from a primitive array to
an object array in order to pass a parameter to the
complexCalculation()
method:
// Assume that temps is a 4000 element double[] double[] temps = readTemps( ); // Turn the double[] into an array of Double objects Double[] objectArray = ArrayUtils.toObject( temps ); List inputList = Arrays.asList( objectArray ); // Assume that some process returns results as a List of Double // objects List outputList = externalLib.complexCalculation( inputList ); // Transform this List of doubles to an array of double primitives Double[] resultObjArray = (Double[]) outputList.toArray( new Double[0] ); double[] result = ArrayUtils.toPrimitive( resultObjArray, Double.NaN );
The primitive array, temps
, is transformed to an
object array using ArrayUtils.toObject( )
, and the
results of our calculation are translated from a list to an array of
primitives using ArrayUtils.toPrimitive( )
. While
an object array can contain a null
element, a
primitive array cannot; the second argument to
ArrayUtils.toPrimitive( )
specifies a double value
to be used if there is a null
in the object array.
In this example, null
values in the object array
are stored as Double.NaN
in our primitive array.
The second argument to ArrayUtils.toPrimitive( )
is optional; if it is not present and a null
value
is present in the object array, a
NullPointerException
is thrown.
ArrayUtils
offers various static methods to
transform between primitive and object arrays. Tables Table 1-2 and Table 1-3 summarize
both the toObject( )
and toPrimitive()
methods.
Table 1-2. The various flavors of ArrayUtils.toObject( )
Return type |
Method signature |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Table 1-3. The various flavors of ArrayUtils.toPrimitive( )
Return type |
Method signature |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Java 1.5 has added a feature called autoboxing, which provides for automatic conversions between primitives and objects. For more information about autoboxing, see http://www.jcp.org/aboutJava/communityprocess/jsr/tiger/autoboxing.html.
Get Jakarta Commons Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.