Convert Integer List to int array

Kamal picture Kamal · Sep 14, 2010 · Viewed 27.1k times · Source

Is there a way, to convert a List of Integers to Array of ints (not integer). Something like List to int []? Without looping through the list and manually converting the intger to int.

Answer

Colin Hebert picture Colin Hebert · Sep 14, 2010

You can use the toArray to get an array of Integers, ArrayUtils from the apache commons to convert it to an int[].


List<Integer> integerList = new ArrayList<Integer>();
Integer[] integerArray = integerList.toArray(new Integer[0]);
int[] intArray = ArrayUtils.toPrimitive(integerArray);

Resources :

On the same topic :