简体   繁体   English

零长度多维数组

[英]Zero length multidimensional array

I know that similar questions have been asked several times and they were nicely answered but... they were about zero length array of 1 dimension like: 我知道类似的问题已被问过几次并且很好地回答了但是......它们是关于1维的零长度数组,如:

int[] array = new int[0];

Seems that there is a purpose for such arrays in case when null should not / cannot be used. 似乎在不应该/不能使用null的情况下存在这种数组的目的。 But why Java allows to create things like that: 但是为什么Java允许创建这样的东西:

int[][][] multiDims = new int[5][0][9];

Of course like in simple 1D case we get nothing from such array if we try to iterate or something and I am asking only because it looks extremely nasty for me. 当然,就像在简单的1D情况下,如果我们尝试迭代或某些东西,我们从这样的数组中得不到任何东西,我只是因为它看起来对我来说非常讨厌。 :-) How much memory is allocated for such a pointless creature? :-)为这样一个毫无意义的生物分配了多少内存?

As for why Java allows this - from the point of view of the language (not the concepts you're trying to express with it), why should it specifically disallow this? 至于为什么Java允许这样做 - 从语言的角度来看(不是你试图用它表达的概念),为什么它应该特别禁止这个呢? If you allow a zero-length array of any type, why specifically disallow a zero-length array of int[9] ? 如果允许任何类型的零长度数组,为什么要特别禁止int[9]的零长度数组? Disallowing it would necessitate more compiler checks to enforce a rule that's basically useless, because even without the rule the behavior is well defined. 禁止它将需要更多的编译器检查来强制执行基本上无用的规则,因为即使没有规则,行为也是明确定义的。

Bottom line, compiler checks are not here to ensure your program makes sense. 底线,编译器检查不是为了确保您的程序有意义。 They're here only to check it's unambiguous. 他们只是在这里检查它是明确的。


Edited to add: 编辑添加:

As pointed out in a comments, such check is not even really possible, since array length is not part of the type information and can be given at run time. 正如评论中指出的那样,这种检查甚至不可能,因为数组长度不是类型信息的一部分,可以在运行时给出。 So, apart of a "special case" when there's int[0] directly in the source code, compiler doesn't even have any means to know whether it is a zero-length array. 因此,除了直接在源代码中有int[0]的“特殊情况”之外,编译器甚至没有任何方法可以知道它是否是零长度数组。

This will create 6 objects - 5 empty arrays and an array containing these arrays. 这将创建6个对象 - 5个空数组和包含这些数组的数组。

Why is it allowed? 为什么允许? For the same reason as in case of 1-dimensional arrays. 出于与一维阵列的情况相同的原因。 If you create an array like this: 如果您创建这样的数组:

int[][][] multiDims = new int[p][q][r];

where each p , q and r can be sometimes 0 , handling these special cases would be very difficult. 其中每个pqr有时可以为0 ,处理这些特殊情况将非常困难。 Instead you get a legitimate object which can be used in a loop (a very short loop - a one that ends immediately, but without errors). 相反,你得到一个合法的对象,可以在一个循环中使用(一个非常短的循环 - 一个立即结束但没有错误的循环)。

The [5] actually does something useful: The outermost array will have five element arrays with length 0. [5]实际上做了一些有用的事情:最外面的数组将有五个长度为0的元素数组。

But you're right about the [9] . 但你对[9]是正确的。 It doesn't do anything: Since all of the five intermediate arrays will be empty no array of length 9 will be created. 它没有做任何事情:由于所有五个中间数组都是空的,因此不会创建长度为9的数组。 Any integer could be put there to exactly the same effect. 可以将任何整数放在那里以达到完全相同的效果。

The language could have been designed so that if there was one level of nested array with dimension set to literal 0 , then all subsequent levels would have to be set to literal 0 as well. 可以设计该语言,以便如果有一个级别的嵌套数组,其维度设置为文字0 ,那么所有后续级别也必须设置为文字0 But that would create yet another a special case in the specification and implementation and the benefit would be very small. 但这将在规范和实施中创造另一个特殊情况,其好处将非常小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM