Index in AP Computer Science A
In AP Computer Science A, an index is the integer position of an element in a String, array, or ArrayList. Indices start at 0 and end at one less than the length, and using any index outside that range throws an out-of-bounds exception.
What is Index?
An index is the number that tells Java where something lives in an ordered collection. Think of it as a seat number, not a count. In AP CSA you use indices in three places. You use them for characters in a String (Topic 1.15), elements in a 1D array (Topic 4.3), and elements in an ArrayList (Topic 4.8).
Java uses zero-based indexing, so the first element sits at index 0. That means a String of length 5 has valid indices 0 through 4. EK 1.15.B.1 states it directly: a String has index values from 0 to one less than its length, and going outside that range causes a StringIndexOutOfBoundsException. Arrays work the same way with arr[i] and arr.length. ArrayLists use get(index), set(index, obj), and size(). The syntax changes across the three structures, but the rule never does. Valid indices are always 0 to length - 1.
Why Index matters in AP® Computer Science A
Index shows up in two units. In Unit 1 (Using Objects and Methods) it supports AP Comp Sci A 1.15.B, where you determine the result of calling String methods like substring, indexOf, and length. Every one of those methods takes or returns an index. In Unit 4 (Data Collections) it supports AP Comp Sci A 4.3.A (accessing 1D array elements) and AP Comp Sci A 4.8.A (calling ArrayList methods and determining their results). Index is the skill that carries over from Strings to arrays to ArrayLists. If you trace indices correctly in Unit 1, you're already most of the way to traversing arrays in Unit 4. Off-by-one index mistakes are among the most common ways points get lost in both MCQ tracing and FRQ code.
Keep studying AP® Computer Science A Unit 4
How Index connects across the course
String substring and indexOf (Unit 1)
substring(from, to) includes index from but stops before index to. So "Programming".substring(3, 7) gives "gram" (indices 3, 4, 5, 6). Treat to as the first index you don't get. indexOf runs the other direction. It hands you back an index, or -1 if the target isn't found, and that -1 is a signal value you're expected to check for.
1D Array Access and Traversal (Unit 4)
An array index is the same idea as a String index with square brackets. A standard loop for (int i = 0; i < arr.length; i++) walks every valid index exactly once. The < matters. Swap it for <= and the loop tries arr[arr.length], which throws an ArrayIndexOutOfBoundsException.
ArrayList Methods (Unit 4)
ArrayList indices follow the same 0 to size() - 1 rule, but the list can change size. When you remove(i), everything after index i shifts left by one. That's why removing items while looping forward can skip elements. The index stayed put while the data moved under it.
2D Arrays: Row and Column Indices (Unit 4)
A 2D array element needs two indices, arr[row][col], and each one is zero-based on its own. The 2018 FRQ Q4 (ArrayTester) built its whole task around "a valid column index," so you had to hold the column index fixed while the row index moved.
Is Index on the AP® Computer Science A exam?
MCQ: Expect code-tracing stems like "What is printed as a result of executing this code segment?" built on substring, indexOf, charAt-style access, or array/ArrayList lookups. A typical trap: String greeting = "Hello " + "Alice".substring(1); gives "Hello lice" (length 10). Then greeting.substring(0, greeting.length() - 2) gives "Hello li". You have to count indices exactly, not guess. Another common pattern uses indexOf("@") and checks == -1 before slicing around that position.
FRQ: Indices drive several released free-response questions. 2017 Q3 (Phrase) and 2021 Q1 (WordMatch) required finding and slicing pieces of a string by index. 2018 Q4 (ArrayTester) and 2019 Q4 (LightBoard) required looping through 2D arrays with row and column indices. To earn the points, your loop bounds have to stay inside 0 to length - 1, and your substring endpoints have to be correct. An out-of-bounds access usually costs you the point for that part of the algorithm.
Index vs Length (length / length() / size())
Length tells you how many elements there are. An index tells you which position one element is in. Because indexing starts at 0, the last valid index is always length - 1, never length itself. "Hello".length() is 5, but "Hello" has no index 5. Its last character is at index 4. Mixing these up is the classic off-by-one error behind most out-of-bounds exceptions.
Key things to remember about Index
An index is a position number, and in Java the first position is always index 0.
The valid indices for a String, array, or ArrayList run from 0 to one less than its length.
The call substring(from, to) includes the character at index from but stops before index to, so it returns to - from characters.
The indexOf method returns the index of the first match, or -1 if the target is not found.
Using an index outside the valid range throws a StringIndexOutOfBoundsException for Strings and an ArrayIndexOutOfBoundsException for arrays.
Removing an element from an ArrayList shifts every later element down one index, which can cause skipped elements in a forward loop.
Frequently asked questions about Index
What is an index in AP Computer Science A?
An index is the integer position of an element in a String, array, or ArrayList. Java starts counting at 0, so the first character of "Hello" is at index 0 and the last is at index 4.
Is the last index of an array equal to its length?
No. The last valid index is length - 1 because indexing starts at 0. An array with arr.length of 10 has indices 0 through 9, and arr[10] throws an ArrayIndexOutOfBoundsException.
Does substring include the end index?
No. substring(from, to) returns characters from index from up to index to - 1. For example, "Hello World!".substring(0, 5) returns "Hello", which is 5 characters at indices 0 through 4.
What's the difference between an index and the length of a String?
Length is the count of characters, and an index is the position of one character. "Alice".length() is 5, but its valid indices are only 0 through 4.
What does indexOf return if the string isn't found?
It returns -1. AP questions often check if (pos == -1) before using the result, because passing -1 into substring would throw a StringIndexOutOfBoundsException.
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.