Java ArrayList.isEmpty() Method

 Posted On  | Yashwant Chavan 

In this tutorial you will learn about Java ArrayList isEmpty() method. isEmpty() method return true if this list contains no elements


 boolean java.util.ArrayList.isEmpty()

Returns true if this list contains no elements.

Java ArrayList isEmpty() example

Refer below steps

  • Constructs an empty list with an initial capacity using new ArrayList<String>(). In this example we have covered two scenarios
  • Before adding elements, .isEmpty() method returns true value. Because list does not contains any elements.
  • After adding some elements, .isEmpty() method returns false value, Because list is not empty now.

import java.util.ArrayList;

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

        ArrayList <String> colors = new ArrayList <String> ();
        System.out.println("isEmpty() before add() ? :- " + colors.isEmpty());
        System.out.println("isEmpty() after add() ? :- " + colors.isEmpty());



isEmpty() before add() ? :- true
isEmpty() after add() ? :- false

© 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0403 seconds.