I want to print all elements in array using foreach loop
int[] array={1,2,3,4,5};
for(int i:array)
System.out.println(array[i]);
And compilers gives me this error/warning
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
but when I print something else
int[] array={1,2,3,4,5};
for(int i:array)
System.out.println("Print something");
It writes "Print something" five times and gives no warning/error.
I assume it has to do something that first element in array has index 0, but I am not sure.
Can someone explain why ?