int[][] a = new int[0][5];
Why the above code is valid in java?
为什么上面的代码在java中有效?
This 2d array is pretty much useless, because the first dimension is ZERO. Logically, if the first dimension is 0, the second dimension should not be bigger than 0.
这个2D阵列几乎没用,因为第一个维度是ZERO。从逻辑上讲,如果第一个维度为0,则第二个维度不应大于0。
I understand we can initiate a 1d empty array.
我知道我们可以启动1d空数组。
2 个解决方案
#1
6
By the JLS, an array expression indicating a size of zero is valid - it is just an expression - so you can legally declare an n-dimensional array that has zero cardinality.
通过JLS,表示大小为零的数组表达式是有效的 - 它只是一个表达式 - 因此您可以合法地声明一个零基数的n维数组。
The only thing that JLS lexer checks (as of Java 8) is whether or not the expression evaluates to zero:
JLS词法分析器检查的唯一内容(从Java 8开始)是表达式是否计算为零:
Next, the values of the dimension expressions are checked. If the value of any DimExpr expression is less than zero, then a NegativeArraySizeException is thrown.
接下来,检查维度表达式的值。如果任何DimExpr表达式的值小于零,则抛出NegativeArraySizeException。
This says nothing of its usefulness, as I'm sure you're aware, any attempt to index into this array will produce an ArrayIndexOutOfBoundsException
since your index location starts at zero.
这没有说明它的用处,因为我相信你知道,任何索引到这个数组的尝试都会产生一个ArrayIndexOutOfBoundsException,因为你的索引位置从零开始。
#2
2
It shouldn't be weird to think as an empty array as something normal. There is a very similar case where we deal with empty arrays all the time.
将空数组视为正常情况并不奇怪。有一个非常类似的情况,我们一直处理空数组。
List<String> names = new ArrayList<String>();
names
is currently an empty array. Wouldn't it be weird to always declare arrays with at least the first element?
名称目前是一个空数组。总是用至少第一个元素声明数组不是很奇怪吗?
List<String> names = new ArrayList<String>(Arrays.asList("John"));
The alternative to new int[0]
being valid syntax would be to set the array to null
when the first element is not available. In this world, we would be expected to regularly check for null
before iterating an array.
new int [0]作为有效语法的替代方法是在第一个元素不可用时将数组设置为null。在这个世界中,我们期望在迭代数组之前定期检查null。
int[] nums = null; // Because empty primitive arrays are "weird"
//typical for-each loop
if(nums != null){
for(int x: nums){
// do something
}
}
//typical for loop
if(nums != null){
for(int c = 0; c < nums.length; c++){
// do something
}
}
Without empty arrays, regular for loops would throw NullPointerException
s all the time. All that said, it is weird to use new int[0][5]
instead of new int[0][]
. I don't think it's worthy of a syntax error, but I think it deserves one of these from the IDE:
没有空数组,常规for循环会一直抛出NullPointerExceptions。总而言之,使用new int [0] [5]而不是new int [0] []是很奇怪的。我认为它不值得语法错误,但我认为它值得IDE中的一个:
#1
6
By the JLS, an array expression indicating a size of zero is valid - it is just an expression - so you can legally declare an n-dimensional array that has zero cardinality.
通过JLS,表示大小为零的数组表达式是有效的 - 它只是一个表达式 - 因此您可以合法地声明一个零基数的n维数组。
The only thing that JLS lexer checks (as of Java 8) is whether or not the expression evaluates to zero:
JLS词法分析器检查的唯一内容(从Java 8开始)是表达式是否计算为零:
Next, the values of the dimension expressions are checked. If the value of any DimExpr expression is less than zero, then a NegativeArraySizeException is thrown.
接下来,检查维度表达式的值。如果任何DimExpr表达式的值小于零,则抛出NegativeArraySizeException。
This says nothing of its usefulness, as I'm sure you're aware, any attempt to index into this array will produce an ArrayIndexOutOfBoundsException
since your index location starts at zero.
这没有说明它的用处,因为我相信你知道,任何索引到这个数组的尝试都会产生一个ArrayIndexOutOfBoundsException,因为你的索引位置从零开始。
#2
2
It shouldn't be weird to think as an empty array as something normal. There is a very similar case where we deal with empty arrays all the time.
将空数组视为正常情况并不奇怪。有一个非常类似的情况,我们一直处理空数组。
List<String> names = new ArrayList<String>();
names
is currently an empty array. Wouldn't it be weird to always declare arrays with at least the first element?
名称目前是一个空数组。总是用至少第一个元素声明数组不是很奇怪吗?
List<String> names = new ArrayList<String>(Arrays.asList("John"));
The alternative to new int[0]
being valid syntax would be to set the array to null
when the first element is not available. In this world, we would be expected to regularly check for null
before iterating an array.
new int [0]作为有效语法的替代方法是在第一个元素不可用时将数组设置为null。在这个世界中,我们期望在迭代数组之前定期检查null。
int[] nums = null; // Because empty primitive arrays are "weird"
//typical for-each loop
if(nums != null){
for(int x: nums){
// do something
}
}
//typical for loop
if(nums != null){
for(int c = 0; c < nums.length; c++){
// do something
}
}
Without empty arrays, regular for loops would throw NullPointerException
s all the time. All that said, it is weird to use new int[0][5]
instead of new int[0][]
. I don't think it's worthy of a syntax error, but I think it deserves one of these from the IDE:
没有空数组,常规for循环会一直抛出NullPointerExceptions。总而言之,使用new int [0] [5]而不是new int [0] []是很奇怪的。我认为它不值得语法错误,但我认为它值得IDE中的一个: