English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java ArrayList isEmpty() method is used to check if the arraylist is empty.
The syntax of the isEmpty() method is:
arraylist.isEmpty()
isEmpty() method has no parameters.
Return true if arraylist contains no elements
Return false if arraylist contains some elements
import java.util.ArrayList; class Main { public static void main(String[] args) { // Create ArrayList ArrayList<String> languages = new ArrayList<>(); System.out.println("Newly created ArrayList: ") + languages); //Check if the ArrayList has any elements boolean result = languages.isEmpty(); // true System.out.println("Is the ArrayList empty? ") + result); //Add some elements to the ArrayList languages.add("Python"); languages.add("Java"); System.out.println("Updated ArrayList: ") + languages); //Check if the ArrayList is Empty result = languages.isEmpty(); // false System.out.println("Is the ArrayList empty? ") + result); } }
Output Results
Newly created ArrayList: [] Is the ArrayList empty? true Updated ArrayList: [Python, Java] Is the ArrayList empty? false
In the above example, we created an ArrayList named languages. Here, we used the isEmpty() method to check if the ArrayList contains any elements.
Initially, the newly created ArrayList does not contain any elements. Therefore, isEmpty() returns true. However, after adding some elements (Python,JavaAfter that, the method returns false.