A beginner in Java here. I'm making a jagged array program where the rows are 10 descending to 1 and they need to be from 0-9
expected output :
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9
I have this
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at Compilations.JaggedArrLab.main(JaggedArrLab.java:23)
error that I have never encountered before so I don't really know what this means nor how to fix it.
Here is my code :
package Compilations;
public class JaggedArrLab {
public static void main(String[] args) {
int array[][] = new int [10][];
//initializing rows from 10-1
array [0] = new int [10];
array [1] = new int [9];
array [2] = new int [8];
array [3] = new int [7];
array [4] = new int [6];
array [5] = new int [5];
array [6] = new int [4];
array [7] = new int [3];
array [8] = new int [2];
array [9] = new int [1];
for (int i = 0; i<array.length; i++){
for (int j=0; j<i + 1; j++){
array[i][j] = i + j;
}
}
for (int i = 0; i<array.length;i++){
for (int j=0; j<i + 1; j++)
System.out.println(array[i][j] + "\t");
}
System.out.println();
}
}