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
1ArrayList<String> listOfBanks = new ArrayList<>();
2int size = listOfBanks.size();
3System.out.println("size of array list after creating: " + size);
1import java.util.ArrayList;
2import java.util.List;
3public class Demo {
4 public static void main(String[] args) {
5 List aList = new ArrayList();
6 aList.add("Apple");
7 aList.add("Mango");
8 aList.add("Guava");
9 aList.add("Orange");
10 aList.add("Peach");
11 System.out.println("The size of the ArrayList is: " + aList.size());
12 }
13}