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

syntax

 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.
package com.technicalkeeda.app;

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());
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        System.out.println("isEmpty() after add() ? :- " + colors.isEmpty());

    }
}

Output

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


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0344 seconds.