Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

IndexOutOfBoundsException in AP Computer Science A

In AP Computer Science A, an IndexOutOfBoundsException is the runtime error Java throws when code tries to access an ArrayList index outside its valid range of 0 through size() - 1. EK 4.9.A.3 names it directly as a risk when you traverse an ArrayList.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is IndexOutOfBoundsException?

An IndexOutOfBoundsException is what Java throws when you ask an ArrayList for a position that doesn't exist. Every ArrayList has valid indexes from 0 up to size() - 1. Ask for list.get(-1) or list.get(list.size()) and the program crashes with this exception. The CED puts it plainly in EK 4.9.A.3: "Attempting to access an index value outside of its range will result in an IndexOutOfBoundsException."

This is a runtime error, not a compile-time error. Your code compiles just fine. The problem only shows up when the program runs and actually reaches the bad index. That's why it tends to hide inside loops. A traversal that uses i <= list.size() instead of i < list.size() looks almost right, but on its very last pass it reaches one index past the end. Think of the list as a row of numbered lockers. The exception is Java refusing to open locker #10 when the row only goes up to #9.

Why IndexOutOfBoundsException matters in AP® Computer Science A

This term lives in Unit 4: Data Collections, specifically Topic 4.9 Traversing ArrayLists. It supports learning objective AP Comp Sci A 4.9.A, "Develop code used to traverse the elements of an ArrayList and determine the results of these traversals." Figuring out the result of a traversal includes spotting when there is no result because the code crashes. EK 4.9.A.3 names the exception outright. Its neighbors in the same LO, EK 4.9.A.2 (skipping elements during deletion) and EK 4.9.A.4 (ConcurrentModificationException in enhanced for loops), describe the other ways a traversal goes wrong. Together they make up the "what can break when you loop over a list" toolkit the exam keeps coming back to.

How IndexOutOfBoundsException connects across the course

ArrayIndexOutOfBoundsException (Unit 4)

This is the same mistake made with an array instead of an ArrayList. Arrays use arr.length and throw ArrayIndexOutOfBoundsException. ArrayLists use list.size() and throw IndexOutOfBoundsException. The bounds rule is identical (0 to length minus 1), so one off-by-one habit can break both kinds of loops.

Removing Elements During a Traversal (Unit 4)

When you call remove(i), every later element shifts left and size() shrinks. A loop that saved the original size in a variable can then run right off the end and throw this exception. A loop that keeps checking i < list.size() stays safe but can skip elements (EK 4.9.A.2). Looping backward from the end avoids both problems.

Enhanced For Loop and ConcurrentModificationException (Unit 4)

An enhanced for loop never touches indexes, so it can't throw an IndexOutOfBoundsException. It trades that risk for a different one. If you change the list's size inside it, you get a ConcurrentModificationException (EK 4.9.A.4). Index loops risk going out of bounds, and for-each loops risk resizing errors.

Traversing an ArrayList (Unit 4)

Every index-based traversal is really a promise to stay between 0 and size() - 1. The standard header for (int i = 0; i < list.size(); i++) keeps that promise automatically. Most IndexOutOfBoundsException questions are just testing whether you can tell when a loop header breaks it.

Is IndexOutOfBoundsException on the AP® Computer Science A exam?

On the multiple-choice section, this term shows up in two ways. Sometimes it's a direct recall question, like "What exception occurs when a traversal loop attempts to access an ArrayList index beyond its valid range?" More often you trace code and choose the correct output, where one answer choice is that the code throws an IndexOutOfBoundsException. Watch for these red flags:

  • i <= list.size() in a loop header
  • starting a backward loop at list.size() instead of list.size() - 1
  • accessing list.get(i + 1) when i can reach the last index
  • removing elements while the loop uses a size saved before the loop started

Fill-in-the-missing-code questions test this too. In a method like curveScores, which adds 5 to every Integer, the working answer uses scores.set(i, scores.get(i) + 5) inside a loop with i < scores.size(). In a method like removeShort, you have to pick the version that removes items without skipping any and without stepping past the end. No released FRQ has used this term verbatim. Still, FRQs that ask you to write ArrayList traversals quietly grade whether your indexes stay in bounds, so a correct loop header matters there too.

IndexOutOfBoundsException vs ArrayIndexOutOfBoundsException

Both mean "you used an index that doesn't exist," but they come from different data structures. ArrayList methods like get, set, and remove throw IndexOutOfBoundsException when the index falls outside 0 to size() - 1. Plain arrays like arr[i] throw ArrayIndexOutOfBoundsException when the index falls outside 0 to arr.length - 1. In Java, ArrayIndexOutOfBoundsException is actually a more specific kind of IndexOutOfBoundsException. A quick way to keep them straight is to match the name to the tool: "Array" in the name means square brackets.

Key things to remember about IndexOutOfBoundsException

  • An IndexOutOfBoundsException is thrown when you access an ArrayList index less than 0 or greater than or equal to size().

  • The valid indexes of an ArrayList run from 0 to size() - 1, so a loop header with i <= list.size() will crash on its final pass.

  • This is a runtime error, which means the code compiles successfully and only fails when the bad index is actually reached.

  • Removing elements during an index-based traversal shrinks the list, which can cause skipped elements or an out-of-bounds index if your loop bound doesn't update.

  • Enhanced for loops can't throw this exception because they don't use indexes, but changing the list's size inside one causes a ConcurrentModificationException instead.

  • Arrays throw ArrayIndexOutOfBoundsException for the same kind of mistake, so the bounds rule you learn for ArrayLists carries straight over to arrays.

Frequently asked questions about IndexOutOfBoundsException

What is an IndexOutOfBoundsException in AP CSA?

It's the runtime error Java throws when you try to access an ArrayList index outside the range 0 to size() - 1. EK 4.9.A.3 in Topic 4.9 names it directly as a traversal risk.

Is IndexOutOfBoundsException a compile-time error?

No. It's a runtime exception, so the code compiles fine and only crashes when it actually reaches the bad index. That's why MCQs often make you trace a loop to the end to find it.

How is IndexOutOfBoundsException different from ArrayIndexOutOfBoundsException?

ArrayList methods like get(5) on a 5-element list throw IndexOutOfBoundsException. Array access like arr[5] on a length-5 array throws ArrayIndexOutOfBoundsException. The array version is a more specific subtype, and both come from the same off-by-one mistake.

Can an enhanced for loop throw an IndexOutOfBoundsException?

No, because an enhanced for loop never uses indexes. However, adding or removing elements inside one can throw a ConcurrentModificationException, which is the risk EK 4.9.A.4 warns about.

Why does removing from an ArrayList in a loop cause errors?

Each remove(i) shifts later elements left and lowers size() by 1. If your loop uses a size saved before the loop, it can run past the new end and throw an IndexOutOfBoundsException. If it rechecks size(), it may skip elements instead. Traversing backward from size() - 1 to 0 avoids both problems.