Join our newsletter for the latest updates.

Java ArrayList size()

The Java ArrayList size() method returns the number of elements present in the arraylist.

The syntax of the size() method is:

arraylist.size()

Here, arraylist is an object of the ArrayList class.


size() Parameters

The size() method does not take any parameters.


size() Return Value

  • returns the number of elements present in the arraylist.

Example: Get the Length of an ArrayList

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // create an ArrayList
        ArrayList<String> languages = new ArrayList<>();

        // insert element to the arraylist
        languages.add("JavaScript");
        languages.add("Java");
        languages.add("Python");
        System.out.println("ArrayList: " + languages);

        // get the number of elements of arraylist
        int size = languages.size();
        System.out.println("Length of ArrayList: " + size);
    }
}

Output

ArrayList: [JavaScript, Java, Python]
Length of ArrayList: 3

In the above example, we have created a arraylist named languages. Here, we have used the size() method to get the number of elements present in the arraylist.