Java ArrayList isempty() Method with Example

In the previous article we have discussed about Java ArrayList subList() Method with Example

In this article we are going to see the use Java ArrayList isEmpty() method along with suitable examples.

Java ArrayList isempty() Method with Example

isEmpty():

This java.util.ArrayList.isEmpty() method is used to check if the arraylist is empty or not. It returns true if the list is empty or it contains 0 element else it returns false.

Syntax:

arrayListName.isEmpty()

Where,

  • arrayListName refers to the name of your ArrayList.

Let’s see different examples to understand it more clearly.

Method-1: Java ArrayList isEmpty() Method – Example with String Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then clear all the elements from the arrayList using clear() method.
  • Check if the arrayList is empty or not using isEmpty() method.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("Hello");
        arr.add("this");
        arr.add("is");
        arr.add("an");
        arr.add("ArrayList");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // clearing all the element from the ArrayList
        arr.clear();
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList are: "+arr);
        // checking if the ArrayList is empty
        System.out.println("Is the ArrayList empty : "+arr.isEmpty());
    }
}
Output:

The elements of ArrayList are: [Hello, this, is, an, ArrayList]
The new elements of ArrayList are: []
Is the ArrayList empty : true

Method-2: Java ArrayList isEmpty() Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then remove one specific elements from the arrayList using remove() method.
  • Print the new ArrayList.
  • Check if the arrayList is empty or not using isEmpty() method.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a ArrayList of Integer datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // clearing all the element from the ArrayList
        arr.remove(2);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // checking if the ArrayList is empty
        System.out.println("Is the ArrayList empty : "+arr.isEmpty());
    }
}
Output:

The elements of ArrayList are: [2, 52, 13, 17, 1]
The elements of ArrayList are: [2, 52, 17, 1]
Is the ArrayList empty : false

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Related Java Programs: