Java 数组的最大维数
出于好奇,Java 中的数组可以有多少维?
Out of curiosity, how many dimensions of an array can you have in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
出于好奇,Java 中的数组可以有多少维?
Out of curiosity, how many dimensions of an array can you have in Java?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
Java语言不限制维数,但JavaVM规范将维数限制为255。
例如,以下代码将无法编译
:错误:(
参考: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:
with 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.")
小实验表明,255 维是最大的。 256 导致编译错误;
屏幕截图
Small experiment shows, that 255 dimensions is maximum. 256 causes compilation error;
The screenshot
严格来说,
java 中只有一维数组是可能的。因为在底层java将多维数组视为数组的数组。
概念证明:http://www.willamette.edu/ ~gorr/classes/cs231/lectures/chapter9/arrays2d.htm
这就是为什么在 Java 中也可能有不规则数组!
Strictly speaking about
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!