Initialize an ArrayList in Java
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++.
Attention reader! Dont stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Below are the various methods to initialize an ArrayList in Java:
-
Initialization with add[]
Syntax:
ArrayList str = new ArrayList[]; str.add["Geeks"]; str.add["for"]; str.add["Geeks"];Examples:
// Java code to illustrate initialization// of ArrayList using add[] methodimport java.util.*;public class GFG {public static void main[String args[]]{// create a ArrayList String typeArrayList gfg = new ArrayList[];// Initialize an ArrayList with add[]gfg.add["Geeks"];gfg.add["for"];gfg.add["Geeks"];// print ArrayListSystem.out.println["ArrayList : " + gfg];}}Output: ArrayList : [Geeks, for, Geeks]Examples: Using shorthand version of this method
// Java code to illustrate initialization// of ArrayList using add[] methodimport java.util.*;public class GFG {public static void main[String args[]]{// create a ArrayList String type// and Initialize an ArrayList with add[]ArrayList gfg = new ArrayList[] {{add["Geeks"];add["for"];add["Geeks"];}};// print ArrayListSystem.out.println["ArrayList : " + gfg];}}Output: ArrayList : [Geeks, for, Geeks] -
Initialization using asList[]
Syntax:
Examples:
// Java code to illustrate initialization// of ArrayList using asList methodimport java.util.*;public class GFG {public static void main[String args[]]{// create a ArrayList String type// and Initialize an ArrayList with asList[]ArrayList gfg = new ArrayList[Arrays.asList["Geeks","for","Geeks"]];// print ArrayListSystem.out.println["ArrayList : " + gfg];}}Output: ArrayList : [Geeks, for, Geeks] -
Initialization using List.of[] method
Syntax:
List obj = new ArrayList[ List.of[Obj A, Obj B, Obj C, ....so on]];Examples:
// Java code to illustrate initialization// of ArrayList using List.of[] methodimport java.util.*;public class GFG {public static void main[String args[]]{// create a ArrayList String type// and Initialize an ArrayList with List.of[]List gfg = new ArrayList[List.of["Geeks","for","Geeks"]];// print ArrayListSystem.out.println["ArrayList : " + gfg];}}Output: ArrayList : [Geeks, for, Geeks] -
Initialization using another Collection
Syntax:
List gfg = new ArrayList[collection];Examples:
// Java code to illustrate initialization// of ArrayList using another collectionimport java.util.*;public class GFG {public static void main[String args[]]{// create another collectionList arr = new ArrayList[];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 gfg = new ArrayList[arr];// print ArrayListSystem.out.println["ArrayList : " + gfg];}}Output: ArrayList : [1, 2, 3, 4, 5]