public class Repetition {
public static void main (String[]a){
int[] x;
x = new int[10];
int i;
int n=0;
for (i=0;i<x.length;i++){
n++;
x[i]=n;
System.out.print(x[i] + " ");
}
i=0;
while (x[i]<x[10]){
System.out.println(x[i]);
i++;
}
}
After running the program, it displays this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Repetition.main(Repetition.java:14)
1 2 3 4 5 6 7 8 9 10 Java Result: 1
Actually, I’m still a newbie in this language. I’m trying to create a program that will assign values into arrays of 10 and displays them and displays again starting from the first array.
I want the output to be like this:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
An
ArrayIndexOutOfBoundsExceptionmeans you tried to use an index into an array, and that index doesn’t exist. For example, if the last valid index in an array is 9 and you use 10 you’ll get this error.This line of code is the problem (the error message tells you the line number):
xis an array of length 10, which means it has indices which go from 0 (zero) to 9.x[10]doesn’t exist so you get the error.