How to Find length/size of an ArrayList in Java with Example

One can use the size() method of java.util.ArrayList to find the length or size of ArrayList in java. The size() method returns an integer equal to a number of elements present in the ArrayList.ArrayList is dynamic in nature. It is different than the length of the Array which is backing the ArrayList that is called the capacity of ArrayList.When you create an object of ArrayList without specifying a capacity, it is created with default capacity which is 10.Since ArrayList is dynamic in nature, it automatically grows in size when the number of element in the  ArrayList reaches a threshold. When ArrayList is first created it is known as empty ArrayList which has size() of zero.If you add elements one by one then size grows one by one.

Read Also :   How to sort an ArrayList in Java with Example

ArrayList size() example in Java 

size() method returns the number of elements present in ArrayList but it is different than capacity which is equal to the length of the underlying array and always greater than the size.

Java Program to find Number of Elements in ArrayList 

import java.util.*;
import java.lang.*;
import java.io.*;

/* Write a program to determine the size/length of the ArrayList*/
public class ArrayListSize
{
    public static void main (String[] args)
    {
        ArrayList<Integer> al=new ArrayList<Integer>();
        System.out.println("Size before adding elements: "+al.size());
        al.add(11);
        al.add(3);
        al.add(5);
        al.add(4);
        al.add(9);
        System.out.println("Size after adding elements: "+al.size());
        al.remove(1);
        al.remove(2);
        System.out.println("Size after remove operations: "+al.size());
        System.out.println("Final ArrayList: ");
        for(int num: al){
            System.out.println(num);
        }
    }
}

Output :
Size before adding elements: 0
Size after adding elements: 5
Size after remove operations: 3
Final ArrayList: 
11
5
9

import java.util.*;
import java.lang.*;
import java.io.*;

/* Program to find size of ArrayList in Java */
public class ArrayListSize
{
        public static void main (String[] args) 
   {
       System.out.println("Java Program to find the length of array list");
       ArrayList<String> listOfCities = new ArrayList<>();
       int size = listOfCities.size();
       System.out.println("size of array list after creating: " + size);
       listOfCities.add("California");
       listOfCities.add("Boston");
       listOfCities.add("New York");
       size = listOfCities.size();
       System.out.println("length of ArrayList after adding elements: " + size);
       listOfCities.clear();
       size = listOfCities.size();
       System.out.println("size of ArrayList after clearing elements: " + size);
   }
}

Output :


Java Program to find the length of array list
size of array list after creating: 0
length of ArrayList after adding elements: 3
size of ArrayList after clearing elements: 0



About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry