Table of contents
1.
Introduction
2.
Examples
3.
Approach – Using size() Method
3.1.
Syntax
4.
Example 1 – Java Program to Determine the Size of an Integer ArrayList
4.1.
Java
5.
Example 2 – Java Program to Determine the Size of a String ArrayList
5.1.
Java
6.
Key Features
7.
Frequently Asked Questions
7.1.
What is the size() method in Java? 
7.2.
Can the size() method be used with other collections in Java? 
7.3.
What will the size() method return if the ArrayList is empty?
7.4.
How can I remove all elements from an ArrayList?
8.
Conclusion
Last Updated: Jul 26, 2024
Easy

Java Program to Find the Length or Size of an ArrayList

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

Introduction

An ArrayList in Java is a resizable array, which can grow as needed. Determining the size or length of an ArrayList is a common task that can be accomplished using the size() method. 

Java Program to Find the Length/Size of an ArrayList

In this article, we'll explore how to find the size of an ArrayList with clear examples and explanations. Let's dive in!

Examples

We will look at two examples:

  1. Determining the size of an Integer ArrayList.
     
  2. Determining the size of a String ArrayList.

Approach – Using size() Method

The size() method of the ArrayList class returns the number of elements present in the list. This method is part of the java.util package and is very straightforward to use. It helps in getting the current number of elements stored in the ArrayList.

Syntax

The syntax for the size() method is:

int size()

This method returns an integer value representing the number of elements in the ArrayList.

Example 1 – Java Program to Determine the Size of an Integer ArrayList

Here’s a simple Java program to find the size of an Integer ArrayList:

  • Java

Java

import java.util.ArrayList;

public class IntegerArrayListSize {

   public static void main(String[] args) {

       // Creating an ArrayList of Integer type

       ArrayList<Integer> intList = new ArrayList<>();

       // Adding elements to the ArrayList

       intList.add(10);

       intList.add(20);

       intList.add(30);

       intList.add(40);

       intList.add(50);

       // Determining the size of the ArrayList

       int size = intList.size();

       // Printing the size of the ArrayList

       System.out.println("The size of the Integer ArrayList is: " + size);

   }

}
You can also try this code with Online Java Compiler
Run Code


Explanation

  1. We import the ArrayList class from the java.util package.
     
  2. We create an ArrayList of Integer type named intList.
     
  3. We add five integer elements to intList.
     
  4. We use the size() method to determine the number of elements in the ArrayList.
     
  5. We print the size of the ArrayList.

Output

The size of the Integer ArrayList is: 5

Example 2 – Java Program to Determine the Size of a String ArrayList

Here’s a simple Java program to find the size of a String ArrayList:

  • Java

Java

import java.util.ArrayList;

public class StringArrayListSize {

   public static void main(String[] args) {

       // Creating an ArrayList of String type

       ArrayList<String> strList = new ArrayList<>();

       // Adding elements to the ArrayList

       strList.add("Apple");

       strList.add("Banana");

       strList.add("Cherry");

       strList.add("Date");

       strList.add("Elderberry");

       // Determining the size of the ArrayList

       int size = strList.size();

       // Printing the size of the ArrayList

       System.out.println("The size of the String ArrayList is: " + size);

   }

}
You can also try this code with Online Java Compiler
Run Code


Explanation

  1. We import the ArrayList class from the java.util package.
     
  2. We create an ArrayList of String type named strList.
     
  3. We add five string elements to strList.
     
  4. We use the size() method to determine the number of elements in the ArrayList.
     
  5. We print the size of the ArrayList.

Output

The size of the String ArrayList is: 5

Key Features

  • Dynamic Sizing: The ArrayList can grow and shrink dynamically as elements are added or removed.
     
  • Ease of Use: The size() method provides an easy way to determine the number of elements in the list.
     
  • Type Safety: ArrayLists can be type-specific, ensuring that only objects of a specified type are stored.

Frequently Asked Questions

What is the size() method in Java? 

The size() method is a part of the ArrayList class in Java. It returns the number of elements currently stored in the ArrayList.

Can the size() method be used with other collections in Java? 

Yes, the size() method is available in other collection classes like HashSet, LinkedList, etc., to determine the number of elements they contain.

What will the size() method return if the ArrayList is empty?

If the ArrayList is empty, the size() method will return 0.

How can I remove all elements from an ArrayList?

You can use the clear() method to remove all elements from an ArrayList. For example:

intList.clear();

Conclusion

Finding the length or size of an ArrayList in Java is a simple task using the size() method. This method is efficient and straightforward, providing a quick way to determine the number of elements in the list. By understanding how to use this method, you can effectively manage and manipulate ArrayLists in your Java programs.

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