Here's my code:
import java.util.Arrays;
public class TwelveInts {
public static void main(String[] args) {
int bucky[]={1,2,3,4,5,6,7,8,9,10,11,12};
System.out.println("First to last: ");
for(int i = 0; i<bucky.length; i++){
System.out.print(bucky[i] + " ");
}
System.out.println("");
System.out.println("Opposite order :) ");
**for(int i = bucky.length; i >=0; i--){**
System.out.print(bucky[i] + " ");
}
}
}
I realize that the line of problematic code is this:
for(int i = bucky.length; i >=0; i--)
It should be written as follows to get rid of the exception:
for(int i = bucky.length - 1; i >=0; i--){
I just don't understand why though. I know that the reason is because it's due to an illegal index, but I just don't understand this exception error in application to this problem. If someone can help me understand why this exception occurs in this problem without the "-1", that would be great. I'm just having a really hard time visualizing it for this.