How to find length/size of Arraylist in java

In this tutorial, we will see how to find length/size of Arraylist in java.
You can use ArrayList’size() method to calculate size of ArrayList.
Let’s understand it with the help of Simple example.

package com.arpit.java2blog;

import java.util.ArrayList;
import java.util.List;

public class ArrayListLengthMain {
    public static void main(String args[])
    {
        List<String> countryList=new ArrayList<>();
        countryList.add("India");
        countryList.add("China");
        countryList.add("Bhutan");
        countryList.add("Nepal");
        System.out.println("Size of Country List is: "+countryList.size());
        System.out.println(countryList);
        countryList.remove(1);
        countryList.remove(2);
        System.out.println("Size of Country List is: "+countryList.size());
        System.out.println(countryList);
    }
}

When you run above program, you will get below output:

Size of Country List is: 4
[India, China, Bhutan, Nepal]
Size of Country List is: 2
[India, Bhutan]

As you can see here, size of ArrayList was 4, then we removed two elements from it and size of ArrayList became two.
That’s how you can find length/size of ArrayList in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.