javac - 在 Java 中声明二维数组

标签 javac

以下是合法的数组声明:

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/

相关文章:

java - Java编译器是否通过删除方法来优化调用一次的方法?

java - 将新类添加到现有 JAR 文件(包含源代码)

javac错误: Class names are only accepted if annotation processing is explicitly requested

android - 构建 android 4.3 master 时出错

javac junit 给出 "error: package org.junit does not exist"

java - 列出javac编译的Java源文件

java - Ant 在 Windows 和 Linux 中的性能较慢

java - 制作 :/bin/javac: Command not found

java - 为什么我的代码会导致编译器标记 "unchecked or unsafe operations"警告?

javac 添加类路径破坏了我的本地类编译