Java ArrayList to Array example shows how to convert ArrayList to array in Java. ArrayList to array example also shows how to convert ArrayList to array of primitive values.
Java ArrayList is a part of the Java Collection framework.
How to convert ArrayList to array?
There are a couple of ways using which you can convert ArrayList to array in Java.
1) Using for loop
Probably this is the simplest way. Loop through the ArrayList elements using for loop and assign elements to an array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.ArrayList; import java.util.Arrays; public class ArrayListToArrayExample { public static void main(String[] args) { ArrayList<String> aListColors = new ArrayList<>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); String[] strColorArray = convertArrayListToArray(aListColors); System.out.println("ArrayList to array: " + Arrays.toString(strColorArray)); } private static String[] convertArrayListToArray(ArrayList<String> aListColors) { //if ArrayList is null, return empty array if(aListColors == null) return new String[0]; /* * Create String array having same size * as ArrayList */ String[] strColorArray = new String[aListColors.size()]; /* * Iterate ArrayList and assign * individual elements to array */ for(int i = 0; i < aListColors.size(); i++) strColorArray[i] = aListColors.get(i); return strColorArray; } } |
Output
1 |
ArrayList to array: [Red, Green, Blue] |
2. Using the toArray method
The ArrayList class has toArray
method which can be used for the conversion.
1 |
public Object[] toArray() |
Please note that this method returns an array of Objects. You need to cast array elements to the appropriate type before using it.
There is an overloaded method toArray
which accepts an array as given below.
1 |
public <T> T[] toArray(T[] a) |
This method returns an array containing all the elements of the ArrayList. The returned array is of the same type which is specified in the argument array.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.ArrayList; import java.util.Arrays; public class ArrayListToArrayExample { public static void main(String[] args) { ArrayList<String> aListColors = new ArrayList<>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); String[] strColorArray = convertArrayListToArray(aListColors); System.out.println("ArrayList to array: " + Arrays.toString(strColorArray)); } private static String[] convertArrayListToArray(ArrayList<String> aListColors) { //if ArrayList is null, return empty array if(aListColors == null) return new String[0]; return aListColors.toArray(new String[aListColors.size()]); } } |
Output
1 |
ArrayList to array: [Red, Green, Blue] |
There is no restriction on the size of the array specified as toArray
method argument. You can even specify an empty array. However, If the argument array has size less than the size of the ArrayList, a new array of the same type is created and returned. If the argument array’s size is greater than the size of the list, the array element that comes immediately after the ArrayList elements is set to null to mark the end of the ArrayList elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.ArrayList; import java.util.Arrays; public class ArrayListToArrayExample { public static void main(String[] args) { ArrayList<String> aListColors = new ArrayList<>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); String[] strColorArray = convertArrayListToArray(aListColors); System.out.println("ArrayList to array: " + Arrays.toString(strColorArray)); } private static String[] convertArrayListToArray(ArrayList<String> aListColors) { //if ArrayList is null, return empty array if(aListColors == null) return new String[0]; return aListColors.toArray(new String[10]); } } |
Output
1 |
ArrayList to array: [Red, Green, Blue, null, null, null, null, null, null, null] |
It is always best practice to specify the array with the same or larger size to reduce the additional overhead of creating a new array.
3) Using Apache Commons
If you are using Apache Commons library and you want to convert ArrayList having wrapper class objects to an array of equivalent primitive values, you are in luck. Use toPrimitive
method of ArrayUtils class to do the conversion.
1 |
static int[] toPrimitive(Integer[] array) |
This method has been overloaded for all the primitive types.
We can use this method in conjunction with toArray
method to convert ArrayList of wrapper objects to an array of primitive values as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class ArrayListToArrayExample { public static void main(String[] args) { ArrayList<Integer> aListNumbers = new ArrayList<>(); aListNumbers.add(new Integer(1)); aListNumbers.add(new Integer(2)); aListNumbers.add(new Integer(3)); int[] intArray = ArrayUtils.toPrimitive( aListNumbers.toArray(new Integer[aListNumbers.size()]) ); System.out.println("ArrayList of Integer to int Array: " + Arrays.toString(intArray)); } } |
Output
1 |
ArrayList of Integer to int Array: [1, 2, 3] |
This example is a part of the Java ArrayList tutorial with examples.
References:
1. ArrayList JavaDoc
2. ArrayUtils class of Apache Commons library