How to get the size of an ArrayList in Java
Using the ArrayList method
size() you can very easily determine the size of an ArrayList in Java. This method returns the current number of items in an ArrayList.
import java.util.ArrayList;
public class ArrayListSize {
public static void main(String[] args) {
//Create ArrayList
ArrayList arraylist = new ArrayList();
//add the elements to ArrayList
arraylist.add(1);
arraylist.add(11);
arraylist.add(22);
//ArrayList size before
System.out.println("Before: "+ arraylist.size());
arraylist.add(33);
//size of the ArrayList after adding an element
System.out.println("After: "+ arraylist.size());
}
}
Runtime:
Before: 3
after: 4