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
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_size.asp
Java ArrayList size() Method
Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms ยท Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of ...
๐ŸŒ
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); } }
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
how to set a maximum size for ArrayList?
Use an if statement and the size() of the ArrayList. More on forum.processing.org
๐ŸŒ forum.processing.org
May 20, 2013
[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
[JAVA] Why is my ArrayList size always 0
Your ArrayList contains 0 elements, so the size is 0. Your top is also initially -1, so the expression top == stack.size() - 1 will evaluate to -1 == 0 - 1, which is always true (until something is added to the stack). You may be confusing size (number of elements in the current ArrayList) with the capacity (maximum number of elements allowed in the current ArrayList). In any case, if you're trying to write a stack for learning purposes, you'd probably get more out of it if you implemented it on a more basic level, and used an array internally instead of an ArrayList which basically has already implemented most of the logic for you. Alternatively, if you just need a stack, you should probably use java.util.Stack instead of re-implementing one. More on reddit.com
๐ŸŒ r/learnprogramming
7
5
February 26, 2017
๐ŸŒ
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 ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. Each ArrayList instance has a capacity.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ length-of-arraylist-java
Java ArrayList Length: How to Get the Size of an ArrayList
February 27, 2024 - Continue reading for more detailed explanations and advanced usage scenarios. ... The size() method is a built-in function in Javaโ€™s ArrayList class. Itโ€™s the most straightforward way to find the length of an ArrayList.
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ Java-array-size-explained-by-example
Java array size, length and loop examples
To get the size of an ArrayList, you use the size() method. Know the difference between the Java array length and the Stringโ€™s length() method.
Find elsewhere
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ size
Java ArrayList size() - Get List Size | Vultr Docs
November 13, 2024 - This code initializes an ArrayList of strings, adds three items, and then uses the size() method to determine how many elements are present. Here, size would output 3. Use the size() method as a condition in loop constructs such as for loops to safely iterate over ArrayList elements. ... import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); for (int i = 0; i < list.size(); i++) { System.out.println("Element at index " + i + ": " + list.get(i)); } } } Explain Code
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-find-length-of-arraylist-in-java
How to find length of ArrayList in Java
September 11, 2022 - You can find the length (or size) of an ArrayList in Java using size() method.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ get-the-size-of-an-arraylist-in-java
Get the Size of an ArrayList in Java
The size of an ArrayList can be obtained by using the java.util.ArrayList.size() method as it returns the number of elements in the ArrayList i.e.
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.

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arraylist.asp
Java ArrayList
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... An ArrayList is like a resizable array. It is part of the java.util package and implements the List interface.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-length-of-array-and-size-of-ArrayList-in-Java
What is the difference between length() of array and size() of ArrayList in Java? - Quora
For arrays the length is fixed at creation; array.length is constant for that instance. ... Arrays and ArrayLists both hold collections of elements in Java, but they expose their element count through different methods (or operators) because they are different types.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 552836 โ€บ java โ€บ find-size-occupied-arraylist-kilobytes
to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist (Java in General forum at Coderanch)
You need to send the items in the array list in a serialized of buffered way and that can not be larger than 4 Mb? If you REALLY want to know the byte size of an object, the "trick" is to convert it to an byte[] and look at the size of it. That is done like this: NOTE: This is serialized object ...
๐ŸŒ
Quora
quora.com โ€บ What-is-the-default-size-of-ArrayList-in-Java
What is the default size of ArrayList in Java? - Quora
Answer (1 of 3): The Default Size of ArrayList is 10 . Once ArrayList reaches its capacity a new ArrayList will be created with new capacity = ( current capacity * 3 /2 ) + 1 . Advantage : Array List is the Best Choice for Retrieval Operations ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ java/util โ€บ java arraylist size
Java ArrayList Size
September 1, 2008 - Let us compile and run the above program, this will produce the following result โˆ’ ... The following example shows the usage of Java ArrayList size() method. We're adding couple of Strings to the ArrayList object using add() method calls per element. Size of the arraylist is printed using size() method.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] why is my arraylist size always 0
r/learnprogramming on Reddit: [JAVA] Why is my ArrayList size always 0
February 26, 2017 -

I've been trying for a while now but I still can't figure out why my arraylist size is always 0. My isFull method always returns true.

public class MyStack<T> {

private ArrayList<T> stack;
private int top;

public MyStack() {
    stack = new ArrayList<T>(50);
    top = -1;
}

public boolean isEmpty() {
    return (top == -1);
}

public boolean isFull() {
    return (top == stack.size() - 1);
}

}