.isEmpty()
Published Mar 10, 2024
Contribute to Docs
The .isEmpty()
function checks if a given ArrayList
is empty. It returns true
if the ArrayList
is empty and false
if it is not empty.
Syntax
myArrayList.isEmpty();
myArrayList
: The name of theArrayList
on which the.isEmpty()
function is called.
Example
The following example demonstrates how to call the .isEmpty()
function on an ArrayList
named fruits
:
import java.util.ArrayList;public class Fruits {public static void main(String[] args) {// Creating a new ArrayListArrayList<String> fruits = new ArrayList<String>();// Using the .isEmpty() functionSystem.out.println("Is fruits empty: " + fruits.isEmpty());fruits.add("kiwi");fruits.add("pineapple");fruits.add("mango");System.out.println("Is fruits empty: " + fruits.isEmpty());}}
The above example will give the following output:
Is fruits empty: trueIs fruits empty: false
Above, the .isEmpty()
function returns true
in the first case because fruits
was initially empty. Then, in the second case, it returns false
as it is called after the three fruits are added to fruits
.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.