Java array ArrayIndexOutOfBoundsException/**
* @author lautturi.com
* Java example: Java example of java.lang.ArrayIndexOutOfBoundsException
*/
import java.util.*;
import java.time.*;
public class Lautturi {
public static void main(String[] args) {
int[] array = new int[3];
array[0] = 1;
array[1] = 3;
array[2] = 5;
// works fine
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);
// Exception : index = array.length out of bounds
for (int i = 0; i <= array.length; i++)
System.out.println(array[i]);
// Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
System.out.println(array[3]);
}
}