Java ArrayList size() Method

Java ArrayList Java ArrayList

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

The syntax of the size() method is:

arraylist.size()

Note:arraylist is an object of the ArrayList class.

Parameter description:

  • None

Return Value

Returns the number of elements in the array.

Example

Get 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("Example");
        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 produces the following output:

网站列表: [Google, Example, Taobao]
动态数组长度: 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 Java ArrayList

Other Extensions