0

I am trying to explicitly declare values in a multidimensional array. I keep getting a multitude of error messages

Here is the offending code:

    int[][] test = new int [6][3];
    test[0] = {1,2,3};
    test[1] = {1,3,2};
    test[2] = {2,3,1};
    test[3] = {2,1,3};
    test[4] = {3,2,1};
    test[5] = {3,1,2};

Is this not allowed in 2 dimensional arrays?
I have read the java doc on arrays

2
  • what error are you getting? Commented Oct 4, 2013 at 7:59
  • error ']' expected, error: illegal start of expressions, error illegal start of expression and error not a statement for the 2nd line Commented Oct 4, 2013 at 7:59

2 Answers 2

3
int [][] test = {
        {1,2,3},
        {1,3,2},
        {2,3,1},
        {2,1,3},
        {3,2,1},
        {3,1,2}};
Sign up to request clarification or add additional context in comments.

Comments

2

You had wrong syntax. You have to specify what to instantize.

int[][] test = new int [6][3];
test[0] = new int[]{1,2,3};
test[1] = new int[]{1,3,2};
test[2] = new int[]{2,3,1};
test[3] = new int[]{2,1,3};
test[4] = new int[]{3,2,1};
test[5] = new int[]{3,1,2};

2 Comments

Thanks Milan, I've spend an inordinately large amount of time on this problem
Additionally if the OP does it this way, they do not need to initialize the second dimension of the array as [3]. They can initialize the array to new int[6][] as the receding lines initialize those elements.

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.