So I have this piece of code declaring a three dimensional array:
public class Matrix {
private static int n;
char[][][] matrix = new char[n][n][2];
Matrix(int n){
n=3;
} }
In the constructor of the class,n gets initialized with a value(let's say 3).
The problem is that when I try to access an element of the matrix(let's say matrix[0][0][0]) I get an "ArrayIndexOutOfBoundsException".
It seems that my matrix has length of 0.
But if I try something like:
char[][][] matrix = new char[3][3][2]
it works just fine,memory is alocated for the matrix.
Also,in my program I use at some point something like this:
char[][] bidimensionalMatrix = new char[n][n];
also,works like a charm, I can access the elements of this one too.
Why is this? I am not allowed to specify the dimension of a three dimensional matrix using a variable?
nis0when the array is constructed.0{}button in the editor for code, not the quote buttonstaticvariable from your constructor as each time the constructor gets called, it will change the value of the thestaticvariable. Either make the variable a member variable, or make the class a singleton.