beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java ArrayList isEmpty() Method example

By Chaitanya Singh | Filed Under: Java Collections

isEmpty() method of java.util.ArrayList class is used for checking whether the list is empty or not. This method returns a boolean value.

public boolean isEmpty()

It returns true if the list is empty otherwise it gives false.

Example

package beginnersbook.com;
import java.util.ArrayList;
public class IsEmptyExample {
  public static void main(String args[]) {
      //ArrayList of Integer Type
      ArrayList<Integer> al = new ArrayList<Integer>();
      //Checking whether the list is empty
      System.out.println("Is ArrayList Empty: "+al.isEmpty());

      //Adding Integer elements
      al.add(1);
      al.add(88);
      al.add(9);
      al.add(17);

      //Again checking for isEmpty
      System.out.println("Is ArrayList Empty: "+al.isEmpty());

      //Displaying elements of the list
      for (Integer num: al) {
      System.out.println(num);
   }
}
}

Output:

Is ArrayList Empty: true
Is ArrayList Empty: false
1
88
9
17

Enjoyed this post? Try these related posts

  1. TreeSet Class in Java with example
  2. Remove Key-value mapping from TreeMap example
  3. Java – Add element at specific index in LinkedList example
  4. Vector in Java
  5. How to find length of ArrayList in Java
  6. How to convert ArrayList to string array in java

Leave a Reply Cancel reply

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

ArrayList Tutorial

  • Java ArrayList

Java ArrayList Methods

  • add(Object obj)
  • add(int index, Object element)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • contains()
  • get()
  • indexOf()
  • ensureCapacity()
  • isEmpty()
  • lastIndexOf()
  • remove()
  • remove(Object obj)
  • trimToSize()
  • set()
  • clone()
  • clear()
  • size()

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap