Return Values
The size() method returns an integer value that represents the number of elements currently stored in the ArrayList. Here are a few key points to note about the return value:
1. If the ArrayList is empty, the size() method will return 0.
2. The size() method returns the actual number of elements present in the ArrayList, not the capacity of the ArrayList.
3. The return value is always a non-negative integer.
It's important to understand that the size() method provides the current size of the ArrayList, which can change dynamically as elements are added or removed from the list.
Examples
Now, we will discuss some practical examples to see how we can use the size() method in Java. We'll start by creating an ArrayList of strings and then move on to different scenarios.
Example 1: Checking the size of an empty ArrayList
Java
import java.util.ArrayList;
public class ArrayListSizeExample1 {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
int size = fruits.size();
System.out.println("Size of the ArrayList: " + size);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Size of the ArrayList: 0
In this example, we create an empty ArrayList called `fruits` and then call the size() method on it. Since the ArrayList is empty, the size() method returns 0.
Example 2: Checking the size of an ArrayList after adding elements
Java
import java.util.ArrayList;
public class ArrayListSizeExample2 {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
int size = fruits.size();
System.out.println("Size of the ArrayList: " + size);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Size of the ArrayList: 3
In this example, we create an ArrayList called `fruits` and add three elements to it using the add() method. After adding the elements, we call the size() method, which returns the current size of the ArrayList, which is 3.
Example 3: Checking the size of an ArrayList after removing elements
Java
import java.util.ArrayList;
public class ArrayListSizeExample3 {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.remove("Banana");
int size = fruits.size();
System.out.println("Size of the ArrayList: " + size);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Size of the ArrayList: 2
In this example, we create an ArrayList called `fruits` with three elements. We then remove the element "Banana" using the remove() method. After removing the element, we call the size() method, which returns the updated size of the ArrayList, which is 2.
Frequently Asked Questions
Can the size() method return a negative value?
No, the size() method always returns a non-negative integer value. If the ArrayList is empty, it will return 0.
Is the size() method the same as the length of an array?
No, the size() method is used specifically for ArrayList objects, while the length property is used for arrays. However, they both serve a similar purpose of providing the number of elements.
Does the size() method affect the performance of an ArrayList?
No, the size() method has a constant time complexity of O(1), which means it takes the same amount of time regardless of the size of the ArrayList. It is a fast and efficient operation.
Conclusion
In this article, we have learned about the ArrayList size() method in Java. We discussed its syntax, return values, and explained practical examples to demonstrate its usage. The size() method is a simple but one of the most importantl tool for determining the number of elements currently stored in an ArrayList. It helps in various scenarios, such as checking if an ArrayList is empty, monitoring the size after adding or removing elements, and making decisions based on the size of the ArrayList.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.