Initialize an ArrayList in Java

Nikhil Soman Sahu
3 min readDec 24, 2022

--

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++.

Initialization with add()

Syntax:

ArrayList<Type> str = new ArrayList<Type>();
str.add("nikhil");
str.add("soman");
str.add("sahu");
// Java code to illustrate initialization
// of ArrayList using add() method

import java.util.*;

public class nkh {
public static void main(String args[])
{

// create a ArrayList String type
ArrayList<String> nkh = new ArrayList<String>();

// Initialize an ArrayList with add()
gfg.add("nikhil");
gfg.add("soman");
gfg.add("sahu");

// print ArrayList
System.out.println("ArrayList : " + gfg);
}
}

OUTPUT:

ArrayList : [nikhil, soman, sahu]

Examples: Using shorthand version of this method

// Java code to illustrate initialization
// of ArrayList using add() method

import java.util.*;

public class nkh {
public static void main(String args[])
{

// create a ArrayList String type
// and Initialize an ArrayList with add()
ArrayList<String> nkh = new ArrayList<String>() {
{
add("nkh");
add("soman");
add("sahu");
}
};

// print ArrayList
System.out.println("ArrayList : " + nkh);
}
}

output:

ArrayList : [nkh, soman, sahu]

Initialization using asList()

Syntax:

ArrayList<Type> obj = new ArrayList<Type>(
Arrays.asList(Obj A, Obj B, Obj C, ....so on));
// Java code to illustrate initialization
// of ArrayList using asList method

import java.util.*;

public class GFG {
public static void main(String args[])
{

// create a ArrayList String type
// and Initialize an ArrayList with asList()
ArrayList<String> gfg = new ArrayList<String>(
Arrays.asList("Geeks",
"for",
"Geeks"));

// print ArrayList
System.out.println("ArrayList : " + gfg);
}
}
Output:
ArrayList : [Geeks, for, Geeks]

Initialization using List.of() method

Syntax :

Syntax:

List<Type> obj = new ArrayList<>(
List.of(Obj A, Obj B, Obj C, ....so on));
// Java code to illustrate initialization
// of ArrayList using List.of() method

import java.util.*;

public class GFG {
public static void main(String args[])
{

// create a ArrayList String type
// and Initialize an ArrayList with List.of()
List<String> gfg = new ArrayList<>(
List.of("Geeks",
"for",
"Geeks"));

// print ArrayList
System.out.println("ArrayList : " + gfg);
}
}
Output:
ArrayList : [Geeks, for, Geeks]

ArrayList is a part of the Java collection framework and it is a class of java.util package. It provides us with 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. This class is found in java.util package. The main advantages of ArrayList are, if we declare an array then it’s needed to mention the size but in ArrayList, it is not needed to mention the size of ArrayList if you want to mention the size then you can do it.

Source : GeeksforGeeks.org

--

--

Nikhil Soman Sahu
Nikhil Soman Sahu

Written by Nikhil Soman Sahu

Sr Software Developer | Spring Boot | Flutter | Dart | Java

No responses yet