Java Array to ArrayList example shows how to convert array to ArrayList in Java. The example also shows how to convert array to ArrayList using various ways.
Java ArrayList is a part of the Java Collection framework.
How to convert array to ArrayList in Java?
There are several ways using which we can convert array to ArrayList in Java.
1) Using the asList method of Arrays class
We can use asList
method of Arrays class to convert array to ArrayList. The asList method returns a fixed sized list backed by the original array. Think of it as a list wrapper around the array.
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 40 41 42 43 |
import java.util.Arrays; import java.util.List; public class ArrayToArrayListExample { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3}; /* * Get fixed size list backed * by the original array using asList method */ List<Integer> list = Arrays.asList(array); System.out.println("List contains: " + list); /* * You cannot add or remove elements * from such list */ //This will give java.lang.UnsupportedOperationException //list.add(4); //This will also give java.lang.UnsupportedOperationException //list.remove(1); /* * You can change the elements though */ list.set(0, 4); System.out.println("List contains: " + list); /* * The changes you make to the list will * also be reflected in the original array */ System.out.println("Array contains: " + Arrays.toString(array)); } } |
Output
1 2 3 |
List contains: [1, 2, 3] List contains: [4, 2, 3] Array contains: [4, 2, 3] |
As you can see from the output, any elements you change in the list is also reflected back to the array. One important thing to note is you cannot change the list structurally i.e. add or remove elements from the list obtained in this way.
If you want ArrayList instead of List object, you can use the ArrayList constructor which accepts List argument as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.ArrayList; import java.util.Arrays; public class ArrayToArrayListExample { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3}; ArrayList<Integer> aList = new ArrayList<>(Arrays.asList(array)); System.out.println("ArrayList contains: " + aList); } } |
Output
1 |
ArrayList contains: [1, 2, 3] |
Since we have created a new ArrayList object using constructor, you can change the ArrayList as you wish and the changes will not be reflected back to the original array.
2) Using the addAll method of Collections class
We can use addAll
method of Collections class to add all the elements of the array to empty ArrayList as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.ArrayList; import java.util.Collections; public class ArrayToArrayListExample { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3}; ArrayList<Integer> aList = new ArrayList<>(); Collections.addAll(aList, array); System.out.println("ArrayList contains:" + aList); } } |
Output
1 |
ArrayList contains:[1, 2, 3] |
3) Using Apache Commons
If you are using Apache Commons library, you can use addAll
method of CollectionUtils class to add all elements of the array to ArrayList as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import org.apache.commons.collections4.CollectionUtils; public class ArrayToArrayListExample { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3}; ArrayList<Integer> aList = new ArrayList<>(); CollectionUtils.addAll(aList, array); System.out.println("ArrayList contains:" + aList); } } |
Output
1 |
ArrayList contains:[1, 2, 3] |
For options 2 and 3, make sure that the ArrayList is empty before adding all the elements of the array. If ArrayList is not empty, the addAll
method will append the elements of array to the ArrayList.
This example is a part of the Java ArrayList tutorial with examples.
References:
1) ArrayList Javadoc
2) Apache Commons CollectionUtils class