The isEmpty() method of ArrayList in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.
Syntax:
list_name.isEmpty()
Parameter: It does not accepts any parameter.
Returns: It returns True if the list list_name has no elements else it returns false. The return type is of datatype boolean.
Error and Exceptions: This method has no error or exceptions.
Program to demonstrate working of isEmpty() in Java:
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<Integer>(10);
boolean ans = arr.isEmpty();
if (ans == true)
System.out.println("The ArrayList is empty");
else
System.out.println("The ArrayList is not empty");
arr.add(1);
ans = arr.isEmpty();
if (ans == true)
System.out.println("The ArrayList is empty");
else
System.out.println("The ArrayList is not empty");
}
}
|
Output:
The ArrayList is empty
The ArrayList is not empty
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
27 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...