Java 数组中的最大维数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4060538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:36:27  来源:igfitidea点击:

Maximum number of dimensions in a Java array

javamultidimensional-array

提问by Adam

Out of curiosity, how many dimensions of an array can you have in Java?

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

回答by kennytm

The Java languagedoes not limit the number of dimensions, but the Java VMspec limits the number of dimensions to 255.

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

For example, the following code will fail to compile:

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

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

with error:

有错误:

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

(Ref: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.1"An array type descriptor is valid only if it represents 255 or fewer dimensions.")

(参考:https: //docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.1“数组类型描述符仅在表示 255 个或更少维度时才有效。 ”)

回答by Muhammad Suleman

Strictly speaking about

严格来说

 Maximum number of dimensions in a Java array

is only one dimensional array is possible in java. because under the hood java treat multidimensional arrays as array of arrays.

在java中只有一维数组是可能的。因为在引擎盖下 java 将多维数组视为数组数组。

Proof of concept: http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm

概念证明:http: //www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm

that's why its possible to have ragged arrays in Java as well!

这就是为什么在 Java 中也可能有参差不齐的数组!

回答by SergeF

Small experiment shows, that 255 dimensions is maximum. 256 causes compilation error;

小实验表明,最多255维。256 导致编译错误;

The screenshot

截图