AP® Computer Science A glossary
IndexOutOfBoundsException
Course: AP® Computer Science A Shows up in: Unit 4: Data Collections
The run-time exception thrown when an ArrayList method gets an index outside its valid range, such as get(size()).
Appears in the course and exam description (CED): 4.9.A.34.8.A.6
Valid indexes for get, set and remove are 0 to size() - 1; add(index, obj) also accepts size(). A saved size that is not updated after removals is a common cause.
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(7);
System.out.println(nums.get(1));
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 Learn it properly, with a checkpoint 4.9 ArrayList Traversals