0

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?

6
  • 2
    n is 0 when the array is constructed. Commented Feb 29, 2016 at 20:17
  • 1
    To expand on Oliver's comment. Un-initialized number fields default to 0 Commented Feb 29, 2016 at 20:18
  • So you're saying that the memory for my matrix is allocated before the constructor of the class steps in?Because n it's initialized in the constructor of this class.Sorry I didn't post the constructor,I will edit Commented Feb 29, 2016 at 20:20
  • 1
    Use the {} button in the editor for code, not the quote button Commented Feb 29, 2016 at 20:25
  • 1
    As a side note, it is generally bad to set a static variable from your constructor as each time the constructor gets called, it will change the value of the the static variable. Either make the variable a member variable, or make the class a singleton. Commented Feb 29, 2016 at 20:33

2 Answers 2

2

You are allowed to define the size of an array with a variable. However, your constructor comes after you have initialized the array, and when the array is initialized, 'n' is 0. Remember, array sizes cannot be changed once created. Instead, you should initialize the array within your constructor.

public class Matrix {   
    private static int n;   
    char[][][] matrix; 

    Matrix(int num){
        n = num;
        matrix = new char[n][n][2];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have to initialize the value of n before you can specify the size of the arrays.

Here is a simple class with multiple sized arrays.

class Matrix {   
    private int width, height, depth;   
    private char[][][] matrix; 

    public Matrix(int width, int height, int depth) {
        matrix = new char[height][width][depth];
    }

    public void set(int i, int j, int k, char value) {
        matrix[i][j][k] = value;
    }

    public char get(int i, int j, int k) {
        return matrix[i][j][k];
    }
}

Sample run

class Main {
  public static void main(String[] args) {
    Matrix m = new Matrix(2, 2, 2);
    m.set(0, 0, 0, 'c');
    System.out.println(m.get(0, 0, 0)); // c
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.