How to find the length or size of an ArrayList in Java

Given an ArrayList in Java, the task is to find the length or size of the ArrayList.

Examples:

Input: ArrayList: [1, 2, 3, 4, 5]
Output: 5

Input: ArrayList: [geeks, for, geeks]
Output: 3

The size of the ArrayList can be determined easily with the help of size() method. This method does not take any parameters and returns an integer value which is the size of the ArrayList.

Syntax:

int size = ArrayList.size();

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the size
// of an ArrayList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Creating object of ArrayList<Integer>
        ArrayList<Integer>
            arrlist = new ArrayList<Integer>();
  
        // Populating arrlist
        arrlist.add(1);
        arrlist.add(2);
        arrlist.add(3);
        arrlist.add(4);
        arrlist.add(5);
  
        // print arrlist
        System.out.println("ArrayList: "
                           + arrlist);
  
        // getting total size of arrlist
        // using size() method
        int size = arrlist.size();
  
        // print the size of arrlist
        System.out.println("Size of ArrayList = "
                           + size);
    }
}

chevron_right


Output:

ArrayList: [1, 2, 3, 4, 5]
Size of ArrayList = 5


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.