In the last tutorial, you learned how to convert an ArrayList to Array in Java. In this guide, you will learn how to convert an array to ArrayList.
Method 1: Conversion using Arrays.asList()
Syntax:
ArrayList<T> arraylist= new ArrayList<T>(Arrays.asList(arrayname));
Example:
In this example, we are using Arrays.asList() method to convert an Array to ArrayList.
Here, we have an array cityNames
with four elements. We have converted this array to an ArrayList cityList
. After conversion, this arraylist has four elements, we have added two more elements to it using add() method.
In the end of the program, we are printing the elements of the ArrayList, which displays 6 elements, four elements that were added to arraylist from array and 2 new elements that are added using add()
method.
import java.util.*; public class JavaExample { public static void main(String[] args) { // Array declaration and initialization String cityNames[]={"Agra", "Mysore", "Chandigarh", "Bhopal"}; // Array to ArrayList conversion ArrayList<String> cityList= new ArrayList<String>(Arrays.asList(cityNames)); // Adding new elements to the list after conversion cityList.add("Chennai"); cityList.add("Delhi"); //print ArrayList elements using advanced for loop for (String str: cityList) { System.out.println(str); } } }
Output:
Agra Mysore Chandigarh Bhopal Chennai Delhi
Method 2: Conversion using Collections.addAll() method
Collections.addAll()
method adds all the array elements to the specified collection. We can call the Collections.addAll
method as shown below. It works just like Arrays.asList()
method however, it is much faster. Conversion using Collections.addAll() method gives better performance compared to asList() method.
String array[]={new Item(1), new Item(2), new Item(3), new Item(4)}; ArrayList<T> arraylist = new ArrayList<T>(); Collections.addAll(arraylist, array);
OR
Collections.addAll(arraylist, new Item(1), new Item(2), new Item(3), new Item(4));
Example:
import java.util.*; public class JavaExample { public static void main(String[] args) { // Array declaration and initialization*/ String array[]={"Hi", "Hello", "Howdy", "Bye"}; //ArrayList declaration ArrayList<String> arraylist= new ArrayList<String>(); // conversion using addAll() Collections.addAll(arraylist, array); //Adding new elements to the converted List arraylist.add("String1"); arraylist.add("String2"); //print ArrayList for (String str: arraylist) { System.out.println(str); } } }
Output:
Hi Hello Howdy Bye String1 String2
Method 3: Convert Array to ArrayList manually
This program demonstrates how to convert an Array to ArrayList without using any predefined method such as asList()
and addAll()
.
The logic of this program is pretty simple, we are iterating the array using for loop and adding the element to the ArrayList on every iteration of the loop. This means, in the first iteration, the first element of the array is added to the ArrayList, in the second iteration second element gets added and so on.
To read the whole array, we are using arr.length
property. The array.length property returns the number of elements in the array. In the following example, since the array contains four elements, this will return 4. Thus we can say that the for loop runs from i=0 to i<4
In the end, we are displaying ArrayList elements using advanced for loop.
import java.util.*; public class JavaExample { public static void main(String[] args) { //ArrayList declaration ArrayList<String> arrayList= new ArrayList<String>(); //Initializing Array String array[] = {"Text1","Text2","Text3","Text4"}; /* array.length returns the number of * elements present in array*/ for(int i =0;i<array.length;i++) { //Adding array elements to the ArrayList arrayList.add(array[i]); } //print ArrayList content for(String str: arrayList) { System.out.println(str); } } }
Output:
Text1 Text2 Text3 Text4
Recommended Articles:
Saheb Bhattu says
In Method 2 ,
can’t we use this method to convert array to arraylist :
arraylist.addAll(array);
as we have used it earlier for list :
arraylist.addAll(list);
Shivam says
Hello,
The method to convert an array to ArrayList using Arrays.asList() works well for any String array, but not with Integer type arrays.
Please help.
Thanks
Kishan says
If you are using the array as “int[] obj=new int[x];” then it will not work because the array list can be created for the Integer class not the int class so it will return a type mismatch error. so take the array as Integer[] obj=new Integer[x];