2

What is wrong with this definition? I want to declare a two dimensional array with those numbers:

int[][] arr = [[1,2,3],[1,2,3],[1,2,3]];

I get this error:

Type mismatch: cannot convert from int to int[]

3 Answers 3

7

Should be:

int[][] arr = {{1,2,3},{1,2,3},{1,2,3}};

More information is available on JLS, see 10.6. Array Initializers:

An array initializer is written as a comma-separated list of expressions, enclosed by braces { and }.

You might also want to have a look at a basic tutorial, it'll help you a lot.

Sign up to request clarification or add additional context in comments.

Comments

3

You should use { and }, instead of [ and ] to initialize the array.

Comments

3

It should be like so:

int[][] arr = {{1,2,3},{1,2,3},{1,2,3}};

Yout need to use curly brakets ({ }).

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.