w3resource

Java: Test an array list is empty or not

Java Collection, ArrayList Exercises: Exercise-18 with Solution

Write a Java program to test whether an array list is empty or not.

Pictorial Presentation:

Java Collection, ArrayList Exercises: Test an array list is empty or not.

Sample Solution:-

Java Code:

import java.util.ArrayList;
import java.util.Collections;
  public class Exercise18 {
  public static void main(String[] args) {
          ArrayList<String> c1= new ArrayList<String>();
          c1.add("Red");
          c1.add("Green");
          c1.add("Black");
          c1.add("White");
          c1.add("Pink");
          System.out.println("Original array list: " + c1);
          System.out.println("Checking the above array list is empty or not! "+c1.isEmpty());
          c1.removeAll(c1);
          System.out.println("Array list after remove all elements "+c1);   
          System.out.println("Checking the above array list is empty or not! "+c1.isEmpty());
   }
}

Sample Output:

Original array list: [Red, Green, Black, White, Pink]                  
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 

Flowchart:

Flowchart: Test an array list is empty or not.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Empty an array list.
Next: Trim the capacity of an array list the current list size.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/collection/java-collection-exercise-18.php