When I try the following code it gives right answer. But when I try to use a.length it throws ArrayIndexOutOfBoundsException. How to make my code to use a.length?
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a;
int n = sc.nextInt();
a = new int[n];
for(int i = 0; i <= a.length; i++) {
a[i] = sc.nextInt();
}
for(int i = a.length; i >= 0; i--) {
System.out.println(a[i]);
}
}
aat the indexa.length, because index range of a will be from0 to (a.length - 1). Since there is no index, that will throw the Exception.