ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
- ArrayList inherits AbstractList class and implements List interface.
- ArrayList is initialized by a size, however the size can increase if collection grows or shrink if objects are removed from the collection.
- Java ArrayList allows us to randomly access the list.
- ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases (see this for details).
- ArrayList in Java can be seen as similar to vector in C++.

Below are the various methods to initialize an ArrayList in Java:
-
Initialization with add()
Syntax:
ArrayList<Type> str = new ArrayList<Type>(); str.add("Geeks"); str.add("for"); str.add("Geeks");Examples:
// Java code to illustrate initialization// of ArrayList using add() methodimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create a ArrayList String typeArrayList<String> gfg =newArrayList<String>();// Initialize an ArrayList with add()gfg.add("Geeks");gfg.add("for");gfg.add("Geeks");// print ArrayListSystem.out.println("ArrayList : "+ gfg);}}chevron_rightfilter_noneOutput:ArrayList : [Geeks, for, Geeks]
Examples: Using shorthand version of this method
// Java code to illustrate initialization// of ArrayList using add() methodimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create a ArrayList String type// and Initialize an ArrayList with add()ArrayList<String> gfg =newArrayList<String>() {{add("Geeks");add("for");add("Geeks");}};// print ArrayListSystem.out.println("ArrayList : "+ gfg);}}chevron_rightfilter_noneOutput:ArrayList : [Geeks, for, Geeks]
-
Initialization using asList()
Syntax:
ArrayList<Type> obj = new ArrayList<Type>( Arrays.asList(Obj A, Obj B, Obj C, ....so on));Examples:
// Java code to illustrate initialization// of ArrayList using asList methodimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create a ArrayList String type// and Initialize an ArrayList with asList()ArrayList<String> gfg =newArrayList<String>(Arrays.asList("Geeks","for","Geeks"));// print ArrayListSystem.out.println("ArrayList : "+ gfg);}}chevron_rightfilter_noneOutput:ArrayList : [Geeks, for, Geeks]
-
Initialization using List.of() method
Syntax:
List<Type> obj = new ArrayList<>( List.of(Obj A, Obj B, Obj C, ....so on));Examples:
// Java code to illustrate initialization// of ArrayList using List.of() methodimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create a ArrayList String type// and Initialize an ArrayList with List.of()List<String> gfg =newArrayList<>(List.of("Geeks","for","Geeks"));// print ArrayListSystem.out.println("ArrayList : "+ gfg);}}chevron_rightfilter_noneOutput:ArrayList : [Geeks, for, Geeks]
-
Initialization using another Collection
Syntax:
List
gfg = new ArrayList<>(collection); Examples:
// Java code to illustrate initialization// of ArrayList using another collectionimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create another collectionList<Integer> arr =newArrayList<>();arr.add(1);arr.add(2);arr.add(3);arr.add(4);arr.add(5);// create a ArrayList Integer type// and Initialize an ArrayList with arrList<Integer> gfg =newArrayList<Integer>(arr);// print ArrayListSystem.out.println("ArrayList : "+ gfg);}}chevron_rightfilter_noneOutput:ArrayList : [1, 2, 3, 4, 5]
- ArrayList of ArrayList in Java
- Initialize HashMap in Java
- KeyPairGenerator initialize() method in Java with Examples
- Initialize a static map in Java with Examples
- Initialize a static Map using Stream in Java
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map in Java using Double Brace Initialization
- How to initialize a list in a single line in Java with a specified value?
- Initialize a list in a single line with a specified value using Java Stream
- How to Fill (initialize at once) an Array in Java?
- How to Initialize and Compare Strings in Java?
- Different ways to Initialize all members of an array to the same value in C
- Best way to initialize empty array in PHP
- Python - Initialize empty array of given length
- Initialize a list in a single line with a specified value
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- Array vs ArrayList in Java
- ArrayList in Java
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

