How to Initialize an Empty ArrayList in Java
In Java, an ArrayList is a resizable array implementation of the List interface. It is widely used for storing and manipulating collections of objects. One of the common tasks when working with ArrayLists is to initialize an empty one. This article will guide you through the process of initializing an empty ArrayList in Java.
1. Using the Default Constructor
The simplest way to initialize an empty ArrayList is by using its default constructor. The default constructor creates an empty ArrayList with an initial capacity of 10. Here’s an example:
“`java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList
System.out.println(“The ArrayList is empty: ” + list.isEmpty());
}
}
“`
In this example, we create an empty ArrayList called `list` using the default constructor. The `isEmpty()` method is used to check if the ArrayList is empty, and it returns `true` in this case.
2. Using the Parameterized Constructor
If you want to specify the initial capacity of the ArrayList, you can use the parameterized constructor. The initial capacity is the number of elements the ArrayList can hold before it needs to be resized. Here’s an example:
“`java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList
System.out.println(“The ArrayList is empty: ” + list.isEmpty());
}
}
“`
In this example, we create an empty ArrayList called `list` with an initial capacity of 10 using the parameterized constructor. The `isEmpty()` method still returns `true`, indicating that the ArrayList is empty.
3. Using the of() Method
Java 9 introduced the `of()` method for creating immutable lists. You can use this method to initialize an empty ArrayList. Here’s an example:
“`java
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
ArrayList
System.out.println(“The ArrayList is empty: ” + list.isEmpty());
}
}
“`
In this example, we create an empty ArrayList called `list` using the `of()` method. The `isEmpty()` method returns `true`, confirming that the ArrayList is empty.
4. Using the ofAll() Method
Similar to the `of()` method, the `ofAll()` method is used to create an immutable list. However, it requires a supplier function that provides the elements for the list. Since we want an empty list, we can use an empty supplier function. Here’s an example:
“`java
import java.util.ArrayList;
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
Supplier
ArrayList
System.out.println(“The ArrayList is empty: ” + list.isEmpty());
}
}
“`
In this example, we create an empty ArrayList called `list` using the `ofAll()` method and an empty supplier function. The `isEmpty()` method returns `true`, indicating that the ArrayList is empty.
In conclusion, initializing an empty ArrayList in Java can be done using various methods, such as the default constructor, parameterized constructor, `of()` method, and `ofAll()` method. Choose the method that best suits your needs and preferences.