Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

1.10. Transforming Between Object Arrays and Primitive Arrays

1.10.1. Problem

You need a way to convert an object array to a primitive array.

1.10.2. Solution

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[].

1.10.3. Discussion

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

Boolean[]

ArrayUtils.toObject( boolean[] array )

Byte[]

ArrayUtils.toObject( byte[] array )

Double[]

ArrayUtils.toObject( double[] array )

Float[]

ArrayUtils.toObject( float[] array )

Integer[]

ArrayUtils.toObject( int[] array )

Short[]

ArrayUtils.toObject( short[] array )

Long[]

ArrayUtils.toObject( long[] array )


Table 1-3. The various flavors of ArrayUtils.toPrimitive( )

Return type

Method signature

Boolean[]

ArrayUtils.toPrimitive( boolean[] array )

Byte[]

ArrayUtils.toPrimitive( byte[] array )

Double[]

ArrayUtils.toPrimitive( double[] array )

Float[]

ArrayUtils.toPrimitive( float[] array )

Integer[]

ArrayUtils.toPrimitive( integer[] array )

Short[]

ArrayUtils.toPrimitive( short[] array )

Long[]

ArrayUtils.toPrimitive( long[] array )


1.10.4. See Also

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.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.