Here is the code to reverse an array
import java.util.*;
class middle {
public static void main(String args[]){
int a[]=new int[]{2,3,65,4,7,8,9};
int c[]=new int[a.length-1];
int k=0;
for(int i=a.length;i>0;i++){
c[k]=a[i];
k++;
}
System.out.println("Reverse of an array");
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
}
}
while running gives Array index out of bound exception:7 where the code is going wrong?
array[array.length]will always be out of bounds. Array indices go from 0 to length-1. See also the official tutorial on arrays.PascalCase;Middlenotmiddle.