Table of contents
1.
Introduction
2.
Syntax
3.
Return Values
4.
Examples
4.1.
Example 1: Checking the size of an empty ArrayList
4.2.
Java
4.3.
Example 2: Checking the size of an ArrayList after adding elements
4.4.
Java
4.5.
Example 3: Checking the size of an ArrayList after removing elements
4.6.
Java
5.
Frequently Asked Questions
5.1.
Can the size() method return a negative value?
5.2.
Is the size() method the same as the length of an array?
5.3.
Does the size() method affect the performance of an ArrayList?
6.
Conclusion
Last Updated: Jul 29, 2024
Easy

ArrayList size() method in Java

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ArrayList is a popular and widely used class in Java that allows you to store and manage a dynamic list of objects. It provides a flexible way to work with collections of elements, as it can grow or shrink in size as needed. The ArrayList class is part of the java.util package and implements the List interface. One of the essential methods provided by the ArrayList class is the size() method, which returns the number of elements currently stored in the list. 

ArrayList size() method in Java

In this article, we will discuss the ArrayList size() method in detail, like its syntax, return values, and practical examples.

Syntax

The syntax for using the size() method in Java is straightforward. Here's how you can call the size() method on an ArrayList object:

int size = arrayList.size();


In this syntax, `arrayList` is the name of the ArrayList object on which you want to call the size() method. The method takes no arguments and returns an integer value representing the number of elements currently present in the ArrayList.

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

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

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

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass