public class ArrayCrash
2: /**
3: * A class to demonstrate that array indexes go from 0 to N-1, not from 1 to N.
4: *
5: * @author Mark Young (A00000000)
6: */
7: public class ArrayCrash {
9: public static void main(String[] args) {
10: // create an array with 5 elements
11: int[] arr = new int[] {1, 2, 3, 4, 5};
13: // try to print it out -- the WRONG WAY!
14: for (int i = 1; i <= 5; ++i) {
15: System.out.println("arr[" + i + "] is " + arr[i]);
16: // prints OK when i is 1 to 4...
17: // .. but CRASHES when i is 5!
18: // Exception in thread "main" ArrayIndexOutOfBoundsException: 5
19: // at ArrayCrash.main(ArrayCrash.java:15)
20: }
21: }
23: }