在 Java 中,什么时候 {a,b,c,...} 数组简写不合适,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17933864/
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
In Java, when is the {a,b,c,...} array shorthand inappropriate, and why?
提问by John P
If you're defining a variable, it appears to be perfectly valid to declare/define a variable as follows:
如果您正在定义一个变量,那么声明/定义一个变量似乎是完全有效的,如下所示:
double[][] output = {{0,0},{1,0}};
But if you're returning a value, it appears to be invalid to write the following:
但是如果你要返回一个值,写以下内容似乎是无效的:
public double[] foo(){
return {0,1,2}
}
I would have thought that internally, both of these would have been performing the same action. Eclipse, at least, disagrees. Does anyone know what the difference is, and where else it can be seen, or why it would be beneficial to accept the former example and reject the latter?
我原以为在内部,这两者都会执行相同的操作。至少 Eclipse 不同意。有谁知道有什么区别,还有什么地方可以看到,或者为什么接受前一个例子并拒绝后者会有好处?
Edit: okay, so it's appropriate when you're initializing, but I don't see any ambiguity... couldn't the JVM interpret the type of variable from the name of the variable (in the case of redefining already initialized variables) or when returning (where the JVM could just look at the return type of the function)? What makes initialization a special case of a rule that would prohibit implicit type? What makes the general rule require explicit type?
编辑:好的,所以当你初始化时它是合适的,但我没有看到任何歧义......JVM不能从变量的名称解释变量的类型(在重新定义已经初始化的变量的情况下)或者在返回时(JVM 可以只查看函数的返回类型)?什么使初始化成为禁止隐式类型的规则的特例?是什么让一般规则要求显式类型?
采纳答案by Jeffrey
It's only acceptable during a declaration. You can, however, use new double[] {0, 1, 2}
.
仅在声明期间可接受。但是,您可以使用new double[] {0, 1, 2}
.
JLS §10.6:
JLS §10.6:
An array initializer may be specified in a declaration, or as part of an array creation expression.
数组初始值设定项可以在声明中指定,或作为数组创建表达式的一部分。
An array creation expression is the new double[] { }
syntax.
数组创建表达式是new double[] { }
语法。
回答by Rohit Jain
You can use braces notation only at the point of declaration, where compiler can infer the type of array from the declaration type.
您只能在声明点使用大括号表示法,编译器可以从声明类型推断数组的类型。
To use it anywhere else you need to use Array Creation Expression:
要在其他任何地方使用它,您需要使用Array Creation Expression:
return new double[] {0,1,2};
回答by committedandroider
One more edge case I found was in the creation of a two dimensional array and initializing the arrays in the two dimensional array
我发现的另一个边缘情况是创建二维数组并初始化二维数组中的数组
So from Jeffrey's response - https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.6- "An array initializer may be specified in a declaration, or as part of an array creation expression", the below code should appear to work because the the array initializer is being used to initialize the array
所以从杰弗里的回应 - https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.6- “可以在声明中指定数组初始值设定项,或作为数组创建表达式”,下面的代码应该可以工作,因为数组初始值设定项用于初始化数组
int[][] grid = new int[3][3];
grid[0] = {1,1,1};
However this didn't work(compilation error), and I had to rewrite this as
但是这不起作用(编译错误),我不得不将其重写为
grid[0] = new int[]{1,1,1};