BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ArrayList isEmpty() Method example

Last Updated: June 12, 2024 by Chaitanya Singh | Filed Under: java

The isEmpty() method of java.util.ArrayList class is used to check whether the list is empty or not. This method returns a boolean value. It returns true if the list is empty, and false if the list contains any elements.

Syntax

public boolean isEmpty()

Example

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Create an empty ArrayList
        ArrayList<String> list = new ArrayList<>();

        // Check if the list is empty or not using isEmpty()
        boolean isEmptyInitially = list.isEmpty();
        System.out.println("Is the list empty initially? " + isEmptyInitially);

        // Add an element to the list
        list.add("Hello");

        // Check if the list is empty after adding an element
        boolean isEmptyAfterAdd = list.isEmpty();
        System.out.println("Is the list empty after adding an element? " + isEmptyAfterAdd);

        // Clear the list (Removing all elements)
        list.clear();

        // Check if the list is empty after clearing
        boolean isEmptyAfterClear = list.isEmpty();
        System.out.println("Is the list empty after clearing? " + isEmptyAfterClear);
    }
}

Output:

Is the list empty initially? true
Is the list empty after adding an element? false
Is the list empty after clearing? true

Explanation

  1. Initialization: An ArrayList is created.
  2. Initial Check: We check whether the list is empty using isEmpty() method, since the list is not yet initialized, it returns true.
  3. Adding Element: We then add an element "Hello" to the list.
  4. Check After Adding: We again check the list using isEmpty() method, this time it returns false as the list contains an element.
  5. Clearing the List: We delete all the elements of the list using clear() method.
  6. Check After Clearing: We again check the list, this time it returns true as the list is empty.
❮ Java ArrayList

Top Related Articles:

  1. How to Find length of an Integer in Java
  2. How to loop HashMap in java
  3. Java Math.sin() Method
  4. How to get the last element of Arraylist?
  5. Java String intern() method

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap