Get size of ArrayList in Java

In this tutorial, we will learn how to Get size of ArrayList in Java.

ArrayList in Java is a resizable-array implementation of the List interface. ArrayList in Java implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Hence, ArrayList is a very good alternative for traditional arrays.

In we want to calculate the size or no. of elements in ArrayList , then this class in Java provides size() method to get size of ArrayList. The size method returns the size or the no. of elements in ArrayList. The syntax of size() method is:

Syntax of size() method:

Returns:
the number of elements in this set

Example:

Output:

 

Related posts

Leave a Comment