Java ArrayList add array example shows how to add all elements of an array to ArrayList in Java. The example also shows how to add array to ArrayList using various ways.
How to add Array to ArrayList in Java?
There are various ways to add an array to ArrayList in Java.
1) Using the addAll method of Collections class
You can use addAll
method of Collections class to add array to ArrayList.
1 |
public static <T> boolean addAll(Collection <? super T> c, T… elements) |
This method adds all specified elements to the argument collection.
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 28 29 30 31 32 33 34 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Collections; public class ArrayListAddArrayExample { public static void main(String[] args) { //Create ArrayList of String objects ArrayList<String> aListDays = new ArrayList<String>(); //Add elements to ArrayList aListDays.add("Sun"); aListDays.add("Mon"); aListDays.add("Tue"); //String array String[] strDays = {"Wed", "Thu", "Fri", "Sat"}; /* * To add elements of an array to ArrayList use, * addAll method of Collection class. * * public static <T> boolean addAll(Collection<? super T> c, T... elements) * */ Collections.addAll(aListDays, strDays); System.out.println("Java ArrayList add array Example"); System.out.println(aListDays); } } |
Output
1 2 |
Java ArrayList add array Example [Sun, Mon, Tue, Wed, Thu, Fri, Sat] |
2) Using the asList method of Arrays class and addAll method of ArrayList
You can use asList
method of Arrays class to convert an array to the List object first.
1 |
public static <T> List<T> asList(T… a) |
This method returns a fixed size List object backed by the original argument array.
Once you get the List object, you can add the list object to the ArrayList using addAll
method of ArrayList.
1 |
public boolean addAll(Collection<? extends E> c) |
This method adds all elements of the specified collection at the end of the ArrayList object.
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 28 29 30 31 32 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; public class ArrayListAddArrayExample { public static void main(String[] args) { //Create ArrayList of String objects ArrayList<String> aListDays = new ArrayList<String>(); //Add elements to ArrayList aListDays.add("Sun"); aListDays.add("Mon"); aListDays.add("Tue"); //String array String[] strDays = {"Wed", "Thu", "Fri", "Sat"}; /* * First, convert array to List using asList method * of Arrays class. Use addAll method of ArrayList to * add all elements of List object to ArrayList. */ aListDays.addAll(Arrays.asList(strDays)); System.out.println("Java ArrayList add array Example"); System.out.println(aListDays); } } |
Output
1 2 |
Java ArrayList add array Example [Sun, Mon, Tue, Wed, Thu, Fri, Sat] |
What is the preferred way to add array to ArrayList?
Using addAll
method of Collections class (approach 1) is preferred over using asList
method of Arrays and addAll
method of ArrayList class (approach 2) because it is faster in terms of performance under most implementation.
This example is a part of the ArrayList in Java tutorial and Array in Java tutorial.
Please let me know your views in the comments section below.