Check Whether ArrayList Is Empty Or Not | Java Programs

Output:

*** Check Whether ArrayList Is Empty Or Not ***

Original array list: [Alee, Anwar, Shakoor, Umar, Sarmad]
--> Checking the above array list is empty or not! false

Array list after remove all elements []
--> Checking the above array list is empty or not! true

Click Here For Java Online Compiler

Solution:

/**
 *
 * @author Alee Ahmed Kolachi
 */
import java.util.ArrayList;

public class ArrayListEmptyOrNot {

    public static void main(String[] args) {
        System.out.println("*** Check Whether ArrayList Is Empty Or Not ***" + "\n");
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Alee");
        arrayList.add("Anwar");
        arrayList.add("Shakoor");
        arrayList.add("Umar");
        arrayList.add("Sarmad");
        System.out.println("Original array list: " + arrayList);
        System.out.println("--> Checking the above array list is empty or not! " + arrayList.isEmpty());
        arrayList.removeAll(arrayList);
        System.out.println();
        System.out.println("Array list after remove all elements " + arrayList);
        System.out.println("--> Checking the above array list is empty or not! " + arrayList.isEmpty());
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon