简体   繁体   中英

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

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.

It should be like so:

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

Yout need to use curly brakets ( { } ).

您应该使用{}而不是[]来初始化数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM