以下是合法的数组声明:
int i[][];
int []j[];
int [][]k;
int[][] l;
但是如果我们在一行中声明数组,我们就会遇到语法错误
int [] []i, []j[], k[], l[][];
ERROR: Syntax Error.
为什么会显示此行为?
最佳答案
问题来了
int [] []i, []j[], k[], l[][];
^^
在 之后的 Java 中,
在声明部分中,您可以在它之后而不是之前声明具有新附加维度的新变量,所以
int a, b[], c[][];
是可能的,和它一样
int a;
ing[] b;
int[][] c;
但是
int a, []b;
不正确。
来自 jls-8.3
More than one field may be declared in a single field declaration by using more than one declarator; the FieldModifiers and Type apply to all the declarators in the declaration.
The declared type of a field is denoted by the Type that appears in the field declaration, followed by any bracket pairs that follow the Identifier in the declarator.
更多信息请访问 10.2. Array Variables
关于javac - 在 Java 中声明二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20475398/