I am currently trying to take the elements of an array and reverse its order in Java. How come I cannot print the elements of the array by counting downwards using a for loop without changing the actual ordering of elements in my array?
private void printArray(int[] array) {
for (int i = array.length; i >= 0; i--){
println(array[i]);
}
}
Array indices start at
0and end atarray.length - 1. Here, you get anArrayIndexOutOfBOundsExceptionsince your first read is past the end of the array (int i = array.length;).Do: