When accessing an array element, one must ensure that the index is less than the length of the array. Using an index that is greater than or equal to the array length causes an ArrayIndexOutOfBoundsException.

Ensure that the index is less than the array length.

The following example causes an ArrayIndexOutOfBoundsException in the final loop iteration.

The condition should be changed as follows to correctly guard the array access.

  • Java API Specification: ArrayIndexOutOfBoundsException.