Easy Tutorial
❮ String Replace Java Modifier Types ❯

Java ArrayList size() Method

Java ArrayList

The size() method is used to return the number of elements in the dynamic array.

The syntax for the size() method is:

arraylist.size()

Note: arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

Returns the number of elements in the array.

Example

Getting the length of the dynamic array:

Example

import java.util.ArrayList;

class Main {
    public static void main(String[] args){

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();

        sites.add("Google");
        sites.add("tutorialpro");
        sites.add("Taobao");
        System.out.println("Website List: " + sites);

        // Get the number of elements in the array
        int size = sites.size();
        System.out.println("Dynamic Array Length: " + size);
    }
}

Executing the above program outputs:

Website List: [Google, tutorialpro, Taobao]
Dynamic Array Length: 3

In the above example, we created a dynamic array named sites and then used the size() method to get the number of elements in the dynamic array.

Java ArrayList

❮ String Replace Java Modifier Types ❯