Saturday, October 18, 2025
HomeLanguagesJavaJava Program to Find the Length/Size of an ArrayList

Java Program to Find the Length/Size of an ArrayList

Given an ArrayList in Java, the task is to write a Java program 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

ArrayList – An ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. However, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

Approach – Using size() method

The size of the ArrayList can be determined easily with the help of the 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:

Example 1 – Java Program to determine the size of an Integer ArrayList

Java




// 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);
    }
}


Output

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

Example 2 – Java Program to determine the size of a String ArrayList

Java




// Java program to find the size
// of an String ArrayList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Creating object of ArrayList<Integer>
        ArrayList<String>
            arrlist = new ArrayList<String>();
  
        // Populating arrlist
        arrlist.add("Lazyroar");
        arrlist.add("a");
          arrlist.add("computer");
        arrlist.add("science");
          arrlist.add("portal");
        arrlist.add("for");
          arrlist.add("geeks");
  
        // 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);
    }
}


Output

ArrayList: [Lazyroar, a, computer, science, portal, for, geeks]
Size of ArrayList = 7
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS