Saturday, September 30, 2023

How to find length/size of ArrayList in Java? Example

You 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 array list. It's different than the length of the array which is backing the ArrayList, which is called the capacity of ArrayList. When you create an object of ArrayList in Java without specifying a capacity, it is created with a default capacity which is 10. Since ArrayList is a growable array, it automatically resizes when the size (number of elements in the array list) grows beyond a threshold. 

Also, when an ArrayList is first created it is called empty ArrayList, and size() will return zero. If you add elements then size grows one by one. 

You can also remove all elements from ArrayList by using a clear() method which will then again make the ArrayList empty as shown in our examples.



ArrayList size() example in Java

Here is our sample Java program to demonstrate how to find the length or size of ArrayList in Java. As discussed it uses the size() method to return a number of elements in the array list but it is different than capacity which is equal to the length of the underlying array and always greater than the size.



The capacity is not publicly exposed, as elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has a constant amortized time cost.

You can also use the trimToSize() method to trim the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance. See Core Java Volume 1 - Fundamentals book, or these free Java courses to learn more about essential collection classes and how they work.

How to find length/size of ArrayList in Java? Example



Java Program to find number of elements in ArrayList

import java.util.ArrayList;

/*
 * Java Program to find the length of ArrayList. *
 */

public class ArrayListDemo{

  public static void main(String[] args) {
    System.out.println("Welcome to Java Program to find the length 
                          of array list");

    ArrayList<String> listOfBanks = new ArrayList<>();
    int size = listOfBanks.size();
    System.out.println("size of array list after creating: " + size);

    listOfBanks.add("Citibank");
    listOfBanks.add("Chase");
    listOfBanks.add("Bank of America");
    size = listOfBanks.size();

    System.out.println("length of ArrayList after adding elements: " + size);

    listOfBanks.clear();
    size = listOfBanks.size();
    System.out.println("size of ArrayList after clearing elements: " + size);
  }

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


From the above example, you can see that the initial size of ArrayList was zero, which increased to 3 after adding 3 elements and it finally becomes zero when you called the clear() method on the ArrayList object. This method removes all elements from the array list making it empty again so that you can reuse it.


Other Java ArrayList tutorials for Programmers
  • How to reverse an ArrayList in Java? (solution)
  • How to create and initialize the ArrayList in one line? (answer)
  • How to declare ArrayList with values in Java? (example)
  • How to convert an ArrayList to String in Java? (example)
  • How to remove duplicates from ArrayList in Java? (example)
  • How convert ArrayList to HashSet in Java? (example)
  • How to get the first and last element of ArrayList in Java? (solution)
  • How to loop over ArrayList in Java? (answer)
  • How to get a range of elements as sublists from ArrayList in Java? (example)
  • How to sort an ArrayList in Java? (answer)
  • How to remove all elements of ArrayList in Java? (answer)
  • How to remove a given element from ArrayList in Java? (answer)

And lastly one question for you, why is ArrayList called dynamic array in Java? 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.