I'm a new programmer and I'm learning multidimensional arrays. I wrote this simple code and I dont know why I get this compile error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Arrays.main(Arrays.java:21)
public class Arrays {
public static void main(String[] args) {
int [][][] a = new int [2][3][4];
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
for(int k = 0; k < a[j].length; k++){
System.out.print(a[i][j][k]);
}
System.out.println();
}
System.out.println();
}
System.out.println();
}
}
If I change the array to be 3x3x3 the code works
public class Arrays {
public static void main(String[] args) {
int [][][] a = new int [3][3][3];
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
for(int k = 0; k < a[j].length; k++){
System.out.print(a[i][j][k]);
}
System.out.println();
}
System.out.println();
}
System.out.println();
}
}
Why?