🌐
W3Schools
w3schools.com › java › ref_arraylist_size.asp
Java ArrayList size() Method
public int size() Java Arrays Tutorial · Java ArrayList Tutorial · ❮ ArrayList Methods · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US · × · If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ·
🌐
TutorialsPoint
tutorialspoint.com › home › java/util › java arraylist size
Java ArrayList Size
September 1, 2008 - The following example shows the usage of Java ArrayList size() method. We're adding couple of Integers to the ArrayList object using add() method calls per element. Size of the arraylist is printed using size() method.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
🌐
Programiz
programiz.com › java-programming › library › arraylist › size
Java ArrayList size()
In this tutorial, we will learn about the Java ArrayList size() method with the help of examples. In this tutorial, we will learn about the ArrayList size() method with the help of examples.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-size-method-in-java-with-examples
ArrayList size() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - Example 2: Here, we use the size() method to get the number of elements in an ArrayList of strings. ... // Java program to demonstrate the use of // size() method for String ArrayList import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Strings ArrayList<String> f = new ArrayList<>(); // Adding elements to the ArrayList f.add("Apple"); f.add("Banana"); f.add("Orange"); // Getting and printing the // size of the ArrayList int s = f.size(); System.out.println("" + s); } }
🌐
W3Schools
w3schools.com › java › tryjava.asp
W3Schools online JAVA editor
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-find-length-of-arraylist-in-java
How to find length of ArrayList in Java
Initial size: 0 Size after few additions: 5 Size after remove operations: 3 Final ArrayList: 1 45 99
🌐
Java67
java67.com › 2016 › 07 › how-to-find-length-size-of-arraylist-in-java.html
How to find length/size of ArrayList in Java? Example | Java67
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) ...
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › arraylist › .size()
Java | ArrayList | .size() | Codecademy
January 27, 2023 - The .size() method of the ArrayList class returns the number of elements in the list. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java — a robust programming language used to create ...
🌐
Baeldung
baeldung.com › home › java › java array › the capacity of an arraylist vs the size of an array in java
The Capacity of an ArrayList vs the Size of an Array in Java | Baeldung
November 11, 2025 - Here, we created an Integer array of size 100, which resulted in the below output ... Technically, the default capacity (DEFAULT_CAPACITY) of a newly created ArrayList is 10. However, Java 8 changed how this initial capacity is used for performance ...
🌐
W3Schools
w3schools.com › java › ref_arraylist_trimtosize.asp
Java ArrayList trimToSize() 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
🌐
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.
🌐
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.
🌐
W3Schools
w3schools.com › java › java_ref_arraylist.asp
Java ArrayList Reference
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 ... A list of all ArrayList methods can be found in the table below. Some methods use the type of the ArrayList's items as a parameter or return value. This type will be referred to as T in the table. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
W3Schools
w3schools.com › java › ref_arraylist_ensurecapacity.asp
Java ArrayList ensureCapacity() Method
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 › 7 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 7 )
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.
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › size
Java ArrayList size() - Get List Size | Vultr Docs
November 13, 2024 - Understanding how to use this method correctly ensures effective handling of dynamic collections in Java programs. In this article, you will learn how to use the size() method with ArrayList in Java. This includes scenarios where knowing the size of a list directly impacts program logic and control flow.
Top answer
1 of 3
3

size of the array is same as number of elements. Only thing to remember is index starts with 0; So last index will be size-1:

import java.util.ArrayList;

public class Chap11Part1 {

    public static void main(String[] args) {
        double average;
        int total = 0;
        ArrayList<Integer> grades = new ArrayList<Integer>();
        grades.add(78);
        grades.add(84);
        grades.add(90);
        for (int i = 0; i < grades.size(); ++i)
            total += grades.get(i);
        average = total / grades.size();
        System.out.println("The average is " + average);
    }
}
2 of 3
0

As has been pointed out, you have a confusion that starts here:

I was instructed that the size of the array is always one more than the number of elements in the array, so in my case, given that I have 3 elements (grades) in my array, I actually have an array size of 4 (similar to how a string works).

Whether this misunderstanding was on part of the instructor explaining things or you misinterpreting the meaning is irrelevant. This understanding is the source of your confusion and ultimately the problem.

You are correct. And ArrayList's size function will return 1 more than something, similar to how Strings work as well. That something is the maximum index. Arrays (and ultimately ArrayLists, which is just a Data Structure implemented on top of an underlying array) have an index value that starts at 0 (not 1 like you typically would when counting). That means, if you had three numbers, you would end up with something like this:

numbers[0] = 10
numbers[1] = 108
numbers[2] = 309

(Ignore the psuedo-code like syntax, pay attention to the values in brackets here)

If I were to get the size (or length with certain types) I would get 3. You can clearly see there are only 3 numbers shown. The "one more than" aspect is one more than the maximum index. The maximum index here is 2, given that we start counting (index-wise) at 0.

This issue is one of the more common I've seen from new developers and it's often referred to as an "off-by-one error" or OBO Error as you want the first element of a list you may try list.get(1) when that's is actually the second element of the list, .get(0) would be the first.

So now...

You have 3 elements in your ArrayList, a call to .size would give (as you should expect) the number 3 in return. Your loop comparisons go like this:

Iteration 1: i = 0; 0 < 3 == true; execute body
Iteration 2: i = 1; 1 < 3 == true; execute body
Iteration 3: i = 2; 2 < 3 == true; execute body
Iteration 4: i = 3; 3 < 3 == false; jump over body

As you can see, your condition is checked "one more than the number of elements" times (in this case, 4) but only 3 times, or the actual number of elements, is the loop body executed or run. If you had accidentally used <= instead, you'd get an OBO Error by accessing "one more than the list contains." Thats why having a grasp on this whole "start at 0" counting system is going to be key, as well as understanding that sizes or locations tend to be one-greater than index values.

If any part of this is more confusing than it should be, let me know and I'll try to fix it up.