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:
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:
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.
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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics