Java ArrayList isEmpty() Method
What is ArrayList?
ArrayList provides us with dynamic arrays. The ArrayLists are resizable in nature , we can add or remove an element at anytime. It is not compulsory to mention size while declaring it. The ArrayList is a part of Java framework and can be found in the java.util pacakge. ArrayList have many inbuilt methods which we can use. One of them is Java ArrayList isEmpty() Method.
Here on this page we will discuss about ArrayList isEmpty() method.
Java ArrayList isEmpty() Method
The isEmpty() method can be found in java.util.ArrayList package. This method is used to check if list is empty or not . It returns true if the ArrayList is empty else it returns false. It’s syntax , parameters, output are discussed below with examples.
isEmpty() Syntax :
ArrayList.isEmpty()
isEmpty() Parameters :
this method does not take any parameters
isEmpty() return value
True: When ArrayList is empty. False: When ArrayList contains any element.
ArrayList isEmpty() Method Examples:
Example 1 :
import java.util.ArrayList; class Main { public static void main(String[] args) { // creating an String ArrayList ArrayList list = new ArrayList<>(); // insert element to the arraylist list.add("Prep"); list.add("Insta"); list.add("Prime"); // check if list is empty or not boolean result = list.isEmpty(); System.out.println("list is empty: "+ result); } }
Output
list is empty: false
Example 2 :
import java.util.ArrayList; class Main { public static void main(String[] args) { // creating an String ArrayList ArrayListlist = new ArrayList<>(); // remove the element Prime from the list boolean result = list.isEmpty(); System.out.println("list is empty: "+ result); } }
Output
list is empty: true
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment