Check if arraylist is empty java: In the previous article, we have seen Java Program to Add Elements into an ArrayList
In this article we are going to see how we can check if an ArrayList is empty or not.
Java Program to Check if ArrayList is Empty
Check if arraylist is empty java: In ArrayList there is an isEmpty()
method which is used to check if arraylist is empty or not. This method returns true is arraylist is not empty else returns false if arraylist is empty.
Sysntax:
Sample_ArrayList.isEmpty();
Where,
Sample_ArrayList
represents an ArrayListisEmpty()
is the predefined ArrayList method
Let’s see different ways to check if an ArrayList is empty or not.
- Java Program to Check if ArrayList is Empty By Using isEmpty() Method of ArrayList
- Java Program to Check if ArrayList is Empty By Using User Defined Method
Method-1: Java Program to Check if ArrayList is Empty By Using isEmpty() Method of ArrayList
Approach:
- Create an arraylist
- Check arraylist is empty or not by using inbuilt method
isEmpty( )
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Creating an empty ArrayList ArrayList<String> arr = new ArrayList<String>(); //checking arraylist is empty or not //without adding any element to the arraylist //so it will print arraylist is empty boolean check=arr.isEmpty(); if (check == true) System.out.println("The arraylist is empty"); else System.out.println("The arraylist is not empty"); // Adding elements to the arrayList arr.add("One"); arr.add("Two"); arr.add("Three"); // Displaying the list System.out.println("ArrayList :"+arr); //checking arraylist is empty or not //after adding elements to the arraylist //so it will print arraylist is empty if(arr.isEmpty()) System.out.println("The arraylist is empty"); else System.out.println("The arraylist is not empty"); } }
Output: The arraylist is empty ArrayList :[One, Two, Three] The arraylist is not empty
Method-2: Java Program to Check if ArrayList is Empty By Using User Defined Method
Approach:
- Create an arraylist
- Run the user-defined method
printEmptyCheck( )
on the empty arraylist to check if it is empty. - Inside user defined method, we will use inbuilt
isEmpty( )
function to check if the arrayList is empty or not.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Creating an empty ArrayList ArrayList<String> arr = new ArrayList<String>(); //calling the printEmptyCheck() user defined method before adding any element to arraylist printEmptyCheck(arr); // Adding elements to the arrayList arr.add("One"); arr.add("Two"); arr.add("Three"); // Displaying the list System.out.println("ArrayList :"+arr); //calling the printEmptyCheck() user defined method printEmptyCheck(arr); } //user defined method to check if arraylist is empty or not //by using predefined method isEmpty() of ArrayList //we will check arraylist is empty or not static void printEmptyCheck(ArrayList arr) { if(arr.isEmpty()) System.out.println("The arraylist is empty"); else System.out.println("The arraylist is not empty"); } }
Output: The arraylist is empty ArrayList :[One, Two, Three] The arraylist is not empty
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: