Jumat, 18 Juli 2014

How to convert List of Integers to int array in Java - Coding Tips




So, you have a List of Integers and you want to convert them into int
array? Yes you read it write, not on
Integer
array
but int array. Though in
most practical purpose,
Integer array can be used in place of int[] because of
autoboxing in Java, y
ou still
need an
int[] if your method accepts it. In Java, you can
not
type cast an
Integer array into
int array. Many Java programmer think about
toArray() method
from
java.util.List to convert a List into Array, but unfortunately toArray() is useless
in most of times. It doesn't allow you to convert
List into
primitive arrays. Though you can convert
List of Integers to array
of
Integers, but not array of primitive int. This is
true for List of all wrapper class e.g. List of
Float, Double, Long, toArray() method can
return array of wrapper class but not primitives. After looking into Java
Collection API, It seems that only traditional for loop or
foreach can help,
which involves iterating over Integer array
a
nd storing them into
int[], but fortunately, I came across Apache
Commons
ArrayUtils class. ArrayUtils can do this
work for us, It has several overloaded methods to convert
Object arrays into primitive arrays. For example, you can convert an array of
Double i.e.
Double[] to primitive double array e.g. double[]. This example
once again reinforced my thinking to include Apache Commons lang and Google's
Guava by default in any Java project, they are rich and effectively complement
standard JDK library.






List of Integer to int array in Java



Java Tip to convert List of Integers to int array in Java exampleHere is complete Java program to convert Integer List into int array. We have
first used
toArray() method to convert List into Array
of same type, and then we use
ArrayUtils to convert
wrapper class array into primitive array. By the way, if you don't like to add
dependencies on your project, you can also use simple for loop to iterate over
Integer array and storing them back into
int[]. here is
the code.

import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;

/**
* Java program to convert List of Integers into int array using Apache commons ArrayUtils class.
*
* @author Javin Paul
*/

public class IntegerListToIntArray {

public static void main(String args[]) {

List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);

System.out.println("List of Integers : " + numbers);

// toArray() can return Integer array but not int array
Integer[] integers = numbers.toArray(new Integer[numbers.size()]);

// ArrayUtils of Apache Commons can change an Object array to primitive array
int[] primitives = ArrayUtils.toPrimitive(integers);

// Let's see what does this int array contains
System.out.println("Array with primitive int : " + Arrays.toString(primitives));
}

}

Output
List of Integers : [1, 2, 3, 4, 5, 6]
Array with primitive int : [1, 2, 3, 4, 5, 6]




As you can see, It's pretty straight forward to convert List of Integers
to int array, though it requires more than one method call to do that. It would
have been much better, if
toArray() can return primitive arrays as
well. On similar note, conversion of Arrays are bit tricky in Java as
int[]
!= Integer[]
, so keep that in mind while writing Java API. Prefer List over Array
for public methods, if you had to accept arrays, try to minimize scope of those
method by making them
private or package-private.





























Source:http://javarevisited.blogspot.com/2013/05/how-to-convert-list-of-integers-to-int-array-java-example-tips.html

Tidak ada komentar:

Posting Komentar