By adding each element of the array
The add() method of the ArrayList class accepts an element and adds it to the current array list. To convert an array to array list using this method −
Get the string array.
Create an empty ArrayList object.
Add each element of the array to the ArrayList.
Print the contents of the array list.
Example
import java.util.ArrayList; import java.util.Iterator; public class ArrayToArrayList { public static void main(String args[]) { String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"}; ArrayList<String> arrayList = new ArrayList<String>(); for(int i = 0; i < stringArray.length; i++) { arrayList.add(stringArray[i]); } System.out.println("Contents of the array list: "); Iterator it = arrayList.iterator(); while(it.hasNext()) { System.out.print(it.next()); } } }
Output
Contents of the array list: JavaFX Java WebGL OpenCV OpenNLP JOGL Hadoop HBase Flume Mahout Impala
Using the asList() method
The asList() method of the ArrayList class accepts an array and returns a List object. To convert an array to an ArrayList −
Get the required array.
Invoke the asList() object by passing the array to is as a parameter and retrieve the List object.
Instantiate an ArrayList class by passing the list object obtained in the previous step.
Print the contents of the ArrayList.
Example
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; public class ArrayToArrayList { public static void main(String args[]) { String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"}; List <String> list = Arrays.asList(stringArray); ArrayList<String> arrayList = new ArrayList(list); System.out.println("Contents of the array list: "); Iterator it = arrayList.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Output
Contents of the array list: JavaFX Java WebGL OpenCV OpenNLP JOGL Hadoop HBase Flume Mahout Impala
Using the addAll method of the Collection class
The addAll() method of the collection class accepts an array list object and an array as parameters and adds the elements of the given array to the array list. Therefore to convert an array to ArrayList using this object −
Get the array.
Create an empty arrayList object.
Invoke the addAll() method of the Collections class by passing the array list and array as parameters.
Print the contents of the array list.
Example
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; public class ArrayToArrayList { public static void main(String args[]) { String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"}; ArrayList<String> arrayList = new ArrayList(); Collections.addAll(arrayList, stringArray); System.out.println("Contents of the array list: "); Iterator it = arrayList.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Output
Contents of the array list: JavaFX Java WebGL OpenCV OpenNLP JOGL Hadoop HBase Flume Mahout Impala