int myArray[]={1,2,3,4,5,6,7,8};
Here, the specified length of the array is 8, since the count starts from 1, but for the index myArray[0] = 1; and so on .... here the number of indices starts at 0. So, in your code snippet
for(int counter = myArray.length - 1; counter >= 0; counter--) {
comes out of the bounds of the array, so it shows you an ArrayIndexOutOfBoundsException .
Sriram pushpak
source share