English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java ArrayList size() method returns the number of elements existing in the arraylist.
The syntax of the size() method is:
arraylist.size()
The size() method does not take any parameters.
Returns the number of elements existing in the arraylist.
import java.util.ArrayList; class Main { public static void main(String[] args) { // Create an ArrayList ArrayList<String> languages = new ArrayList<>(); //Insert elements into the array list languages.add("JavaScript"); languages.add("Java"); languages.add("Python"); System.out.println("ArrayList: "); + languages); //Get the number of elements in the array list int size = languages.size(); System.out.println("Number of elements in ArrayList: "); + size); } }
Output Result
ArrayList: [JavaScript, Java, Python] Number of elements in ArrayList: 3
In the above example, we created an array list named languages. Here, we used the size() method to get the number of elements existing in the array list.