- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How many ways are there to convert an Array to ArrayList in Java?
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
- Related Articles
- How many ways to synchronize an ArrayList in Java?
- How many ways are there to register a driver in Java?
- How many ways are there to initialize a final variable in java?
- How do you convert an ArrayList to an array in Java?
- Convert an ArrayList to an Array with zero length Array in Java
- How many ways are there to initialize the instance variables of a class in java?
- In how many ways we can convert a String to a character array using Java?
- Convert an ArrayList of String to a String array in Java
- Convert an ArrayList to HashSet in Java
- How to convert Java array or ArrayList to JsonArray using Gson in Java?
- How to convert array to arraylist in android?
- How to create an ArrayList from an Array in Java?
- Are there any ways to Manipulate Strings in Java.
- How many ways to make an object eligible for GC in Java?
- How to convert a comma separated String into an ArrayList in Java?
