When the index doesn't exist
A list with three values has the indexes 0, 1 and 2, and no others. Asking for any other index stops the program with an error:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> chores = new ArrayList<>();
chores.add("do the dishes");
chores.add("water the plants");
chores.add("take out the trash");
System.out.println(chores.get(0));
System.out.println(chores.get(3));
System.out.println("All done");
}
}
do the dishesException in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
at Main.main(Main.java:10)The program compiled without a complaint: javac can't know how many values a list will hold when the program runs. The error comes while it runs, as an exception, and the program stops on the spot. That's why All done is never printed. Java's message says what went wrong and where:
IndexOutOfBoundsExceptionis the kind of error: an index outside the list.Index 3 out of bounds for length 3gives the index that was asked for, and how many values the list had.at Main.main(Main.java:10)is where it happened: line 10, in the methodmain.
In a real run, Java also prints a few at lines from inside its own classes, where the index was checked. The one to look for is the at Main line: that's your code.
A negative index crashes the same way (Index -1 out of bounds for length 3), and so does every index on an empty list (Index 0 out of bounds for length 0).
Avoiding it
The largest index is always one less than the number of values. A common cause of this crash is counting from 1, as people do, instead of from 0: get(3) for the third value, or a loop that runs one round too far. When the index in the message equals the length, as in Index 3 out of bounds for length 3, look for exactly that mistake.
The next step shows how to ask a list how many values it holds, so a program can check an index before it uses it.