Write a Java program how do you find the number of elements present in an ArrayList


First, an ArrayList col_list is created and some elements are added to it using the add method.

Then, the size method is called on the col_list ArrayList to get the number of elements present in it. The result is printed using the System.out.println method.

Finally, the output is printed which displays the given ArrayList and the number of elements present in it.

Source Code

import java.util.ArrayList;
public class FindNumber_Elements
{
    public static void main(String[] args)
    {
		ArrayList<String> col_list = new ArrayList<String>();
		col_list.add("Blue");
		col_list.add("Green");
		col_list.add("Pink");
		col_list.add("Black");
		col_list.add("Red");
		col_list.add("orange"); 
		col_list.add("White"); 
		System.out.println("Given ArrayList : "+col_list); 
		System.out.println("Number of Elements Present in an ArrayList : "+col_list.size());
    }
}

Output

Given ArrayList : [Blue, Green, Pink, Black, Red, orange, White]
Number of Elements Present in an ArrayList : 7

Example Programs