AnnieOK
AnnieOK

Reputation: 1390

Defining a two dimensional array

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[]

Upvotes: 2

Views: 138

Answers (3)

3751_Creator
3751_Creator

Reputation: 664

It should be like so:

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

Yout need to use curly brakets ({ }).

Upvotes: 3

Mestica
Mestica

Reputation: 1529

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

Upvotes: 3

Maroun
Maroun

Reputation: 95968

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.

Upvotes: 7

Related Questions