This exception is thrown when code accesses an array (or an array-backed structure reporting through the same exception) with an index that is negative or greater than or equal to its length. Modern messages are admirably complete: Index 5 out of bounds for length 5 gives the index used and the valid range, 0 to 4. The bug is never in the array; it is in how the index was computed, or in an assumption that the array would be bigger than it is.
<= array.length instead of <, or starting a “human” count at 1 against a 0-based structure, so the final iteration steps one past the end.for (String s : arr)) or iterating with the collection's own size removes the entire class of loop-bound errors.arr[0] patterns with a length test whose branch handles the empty case as a real outcome, an empty result is usually a legitimate answer, not an impossibility.Why index 5 with length 5? Isn't 5 the last one? No: indices run 0 through length-1, so a length-5 array's last index is 4. An index equal to the length is the single most common form of this error, the loop bound written with <= instead of <.
I get this from a library call, not my own array code, why? Some library methods index internally based on arguments you supply (ranges, pages, sublists). The index arithmetic is still yours; check the from/to values passed in, especially end-exclusive conventions.
Does it happen with Lists too? Lists throw IndexOutOfBoundsException (the parent type, without “Array”). The causes and fixes are the same; seeing the Array variant tells you an actual array is involved somewhere in the call.