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

To find the length or size of ArrayList in java one can use java.util.ArrayList size() method.

According to ArrayList Oracle docs, size() method returns the number of elements present in the ArrayList.

ArrayList size() method is different from capacity of ArrayList. Each object of the ArrayList has a capacity. The capacity is the size of the array used to store the elements in the list. Capacity will always atleast as large as the size of the list. As we know ArrayList is dynamic in nature. The capacity of the ArrayList grows automatically, as elements are added to it. When you do not specify capacity while creating the object of ArrayList ,then the object is created with default capacity 10.

I have shared two examples of finding length/size of ArrayList
1. Integer ArrayList object
2. String ArrayList  object

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

ArrayList size() example in Java 

When you create an Object of an ArrayList, it is created as empty ArrayList  and it's size() will be zero.If you add elements one by one then size grows one by one.

1. Java Program to find Length/Size of Integer ArrayList


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

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

Output :
Size before adding elements: 0
Size after adding elements: 5
Size after removing elements: 3
Resulting ArrayList: 
11
5
9
 

2. Java Program to find Length/Size of String ArrayList


import java.util.*;
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 size of ArrayList"); 
 
       // Create an String ArrayList Object 
       ArrayList<String> listOfCities = new ArrayList<>(); 
 
       int size = listOfCities.size(); 
       // Print initial size of ArrayList   
       System.out.println("size of ArrayList after creation: " + size);
 
       // Adding elements to ArrayList Object 
       listOfCities.add("California");
       listOfCities.add("Boston");
       listOfCities.add("New York"); 
 
       size = listOfCities.size(); 
       /* Print size of ArrayList 
          after adding elements */  
       System.out.println("size of ArrayList after adding elements: " + size); 
 
       // clear() method removes all elements
       listOfCities.clear(); 
 
       size = listOfCities.size();
       System.out.println("size of ArrayList after clearing elements: " + size);
   }
}

Output :


Java Program to find the size of ArrayList
size of ArrayList after creation: 0
size 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