The size member function.

myList.size();

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Answer from Adam Reed on Stack Overflow
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-find-length-of-arraylist-in-java
How to find length of ArrayList in Java
September 11, 2022 - Initial size: 0 Size after few additions: 5 Size after remove operations: 3 Final ArrayList: 1 45 99
Discussions

Length of ArrayList in Java - Stack Overflow
I hear you say ... the array's length is zero! Yes, but that is not the backing array! The toArray() method allocates a new array and copies the List contents into that array. It does NOT return the actual backing array. ... Maybe you should encapsulate your ArrayList in a class and add another ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Java] size vs. length
Arrays are fixed length so they use a field/variable that gets set on initialization. Lists are variable length so they need a method to figure out their own size. More on reddit.com
๐ŸŒ r/learnprogramming
7
3
July 16, 2013
Java: Is Arraylist better than Arrays?
The biggest difference is that you can add to and remove from a list, whereas arrays have a fixed size. More on reddit.com
๐ŸŒ r/learnprogramming
28
19
June 23, 2024
[Java] How to write .size() method for an ArrayList?
Look ar ArrayList's implementation, create your own class, and implement the most useful methods(add,get,remove,size). It should be pretty straightforward. As you'll see, there is a private variable size, that increases and decreases when you add/remove an object. More on reddit.com
๐ŸŒ r/learnprogramming
16
7
April 30, 2013
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ Java-array-size-explained-by-example
Java array size, length and loop examples
The size or length count of an array in Java includes both null and non-null characters. Unlike the String and ArrayList, Java arrays do not have a size() or length() method, only a length property.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_size.asp
Java ArrayList size() Method
compare() equals() sort() fill() length Java ArrayList Methods ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-find-the-length-size-of-an-arraylist
Java Program to Find the Length/Size of an ArrayList - GeeksforGeeks
July 11, 2025 - This method does not take any parameters and returns an integer value which is the size of the ArrayList. ... // Java program to find the size // of an ArrayList import java.util.*; public class GFG { public static void main(String[] args) throws Exception { // Creating object of ArrayList<Integer> ArrayList<Integer> arrlist = new ArrayList<Integer>(); // Populating arrlist arrlist.add(1); arrlist.add(2); arrlist.add(3); arrlist.add(4); arrlist.add(5); // print arrlist System.out.println("ArrayList: " + arrlist); // getting total size of arrlist // using size() method int size = arrlist.size(); // print the size of arrlist System.out.println("Size of ArrayList = " + size); } }
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arraylist-size-method-in-java-with-examples
ArrayList size() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - Example 1: Here, we will use the size() method to get the number of elements in an ArrayList of integers. ... // Java program to demonstrate the use of // size() method for Integer ArrayList import java.util.ArrayList; public class GFG { public ...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ length-of-arraylist-java
Java ArrayList Length: How to Get the Size of an ArrayList
February 27, 2024 - Always ensure that your ArrayLists are well-structured and appropriately sized for your needs. And remember, the size() method is your friend when you need to determine the length of an ArrayList, even in a multi-dimensional context. Java 8 introduced a powerful new concept called Streams, which can also be used to determine the length of an ArrayList.
๐ŸŒ
Processing
processing.org โ€บ reference โ€บ arraylist
ArrayList / Reference / Processing.org
An ArrayList is a resizable-array implementation of the Java List interface. It has many methods used to control and search its contents. For example, the length of the ArrayList is returned by its size() method, which is an integer value for the total number of elements in the list.
Top answer
1 of 3
4

ArrayList.size() will give the current size.That's why hash.size() giving you the current size of your ArrayList hash. It will not give you the capacity.
You just initialized the list. Have not add any elements to your arraylist, that's why its giving 0.

2 of 3
2

There is no such method in the ArrayList API. The capacity of an ArrayList is hidden by design.

However, I think that your question is based on a misunderstanding.

How do I find the size of an ArrayList in Java? I do not mean the number of elements, but the number of indexes.

In fact, the size of a List, the number of elements in a List, and the number of indexes (i.e. indexable positions) for a List ... are all the same thing.

The capacity of an ArrayList is something different. It is the number of elements that the object could contain, without reallocating the list's backing array. However, the fact that the list has a capacity N does NOT mean that you can index up to N - 1. In fact, you can only index up to size() - 1, irrespective of the capacity.


Now to deal with your examples:

ArrayList list = new ArrayList(5);
System.out.println(list.size());

This prints out zero because the list has zero elements. The ArrayList() and ArrayList(int) constructors both create and return lists that are empty. The list currently has space for 5 elements (because you gave it an initial capacity of 5) but you can't index those slots.

System.out.println(list.toArray().length);

This prints zero because when you copy the list's contents to an array (using toArray()), the array is the same size as the list. By definition.

This does not mean that the list's backing array has changed. On the contrary, it is still big enough to hold 5 elements without reallocation ... just like before.

But ... I hear you say ... the array's length is zero!

Yes, but that is not the backing array! The toArray() method allocates a new array and copies the List contents into that array. It does NOT return the actual backing array.

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-the-difference-between-the-size-of-arraylist-and-length-of-array-in-java
What is the difference between the size of ArrayList and length of Array in Java?
July 30, 2019 - JavaObject Oriented ProgrammingProgramming ยท ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array. It is the total space allocated during ...
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ remove-duplicates-from-sorted-array
Remove Duplicates from Sorted Array - LeetCode
Can you solve this real interview question? Remove Duplicates from Sorted Array - Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place [https://en.wikipedia.org/wiki/In-place_algorithm] such that each unique element appears only once.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ array-vs-arraylist-in-java
Array vs ArrayList in Java - GeeksforGeeks
July 23, 2025 - In Java, an Array is a fixed-sized, homogenous data structure that stores elements of the same type whereas, ArrayList is a dynamic-size, part of the Java Collections Framework and is used for storing objects with built-in methods for manipulation.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays.asp
Java Arrays
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Fac
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - This class is a member of the Java Collections Framework. ... Constructs an empty list with the specified initial capacity. ... Constructs an empty list with an initial capacity of ten. ... Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. ... Trims the capacity of this ArrayList instance to be the list's current size.