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;
2public class ArrayListSizeMethodExample
3{
4 public static void main(String[] args)
5 {
6 ArrayList<Integer> al = new ArrayList<Integer>();
7 int s = al.size();
8 System.out.println("size of ArrayList: " + s);
9 al.add(25);
10 al.add(64);
11 al.add(73);
12 s = al.size();
13 System.out.println("size of ArrayList after adding elements: " + s);
14 al.clear();
15 s = al.size();
16 System.out.println("size of ArrayList after clearing elements: " + s);
17 }
18}
1import java.util.ArrayList;
2public class ArrayListSizeMethodExample
3{
4 public static void main(String[] args)
5 {
6 ArrayList<String> al = new ArrayList<String>();
7 int s = al.size();
8 System.out.println("size of ArrayList: " + s);
9 al.add("red");
10 al.add("blue");
11 al.add("green");
12 s = al.size();
13 System.out.println("size of ArrayList after adding elements: " + s);
14 al.clear();
15 s = al.size();
16 System.out.println("size of ArrayList after clearing elements: " + s);
17 }
18}