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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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]
[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.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.