In order count number of Item in a list in java , we use ArrayList.size() method . ArrayList size() method return number of element in the arraylist.
import java.util.ArrayList;
public class CountElement {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
// add element in arraylist
arr.add("c");
arr.add("php");
arr.add("html");
arr.add("java");
arr.add("delhi");
arr.add("INDIA");
// print arraylist
System.out.println("arrayList is = " + arr);
/*
* Returns the number of elements in this list.
*/
System.out.println(" size = "+arr.size());
}
}
Output:
arrayList is = [c, php, html, java, delhi, INDIA]
size = 6
References :