reverse for loop for array countdown
You declared array on integers of 10 elements
. And you are iterating from i=0 to i=10
and i=10 to i=0
that's 11 elements
. Obviously it's an index out of bounds error
.
Change your code to this
public class Reverse { public static void main(String [] args){ int i, j; System.out.print("Countdown\n"); int[] numIndex = new int[10]; // array with 10 elements. for (i = 0; i<10 ; i++) { // from 0 to 9 numIndex[i] = i;// element i = number of iterations (index 0=0, 1=1, ect.) } for (j=9; j>=0; j--){ // from 9 to 0 System.out.println(numIndex[j]);//indexes should print in reverse order from here but it throws an exception? } }}
Remember indices starts from 0.
.