Java 数组的最大维数

发布于 2024-09-30 06:47:22 字数 28 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

焚却相思 2024-10-07 06:47:22

Java语言不限制维数,但JavaVM规范将维数限制为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 个或更少维度时才有效。” )

The Java language does not limit the number of dimensions, but the Java VM spec limits the number of dimensions to 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.")

少钕鈤記 2024-10-07 06:47:22

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

屏幕截图

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

The screenshot

审判长 2024-10-07 06:47:22

严格来说,

 Maximum number of dimensions in a Java array

java 中只有一维数组是可能的。因为在底层java将多维数组视为数组的数组。

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

这就是为什么在 Java 中也可能有不规则数组!

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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文