1import java.util.ArrayList;
2
3/*
4 * Java Program to find the length of ArrayList. *
5 */
6
7public class ArrayListDemo{
8
9 public static void main(String[] args) {
10 System.out.println("Welcome to Java Program to find the length of array list");
11
12 ArrayList<String> listOfBanks = new ArrayList<>();
13 int size = listOfBanks.size();
14 System.out.println("size of array list after creating: " + size);
15
16 listOfBanks.add("Citibank");
17 listOfBanks.add("Chase");
18 listOfBanks.add("Bank of America");
19 size = listOfBanks.size();
20
21 System.out.println("length of ArrayList after adding elements: " + size);
22
23 listOfBanks.clear();
24 size = listOfBanks.size();
25 System.out.println("size of ArrayList after clearing elements: " + size);
26 }
27
28}
29Output
30Welcome to Java Program to find length of array list
31the size of array list after creating: 0
32the length of ArrayList after adding elements: 3
33the length of ArrayList after clearing elements: 0