出于好奇,您在Java中可以拥有数组的多少个维度?

最佳答案

Java语言不限制维数,但是Java VM规范将维数限制为255。

例如,以下代码将无法编译:

class Main {
    public static void main(String[] args) {
        final int[][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][] x;
    }
}


错误:

1.java:18: error: array type has too many dimensions
                 [][][][][][][][][][][][][][][][] x;
                                                  ^
1 error


(引用:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.1“数组类型描述符仅在表示255个以下维时才有效。”)

10-04 23:31