java 在构造函数中使用数组常量时出现编译器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3078441/
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
Getting compiler error while using array constants in the constructor
提问by jai
public class Sonnet29 implements Poem {
private String[] poem;
public Sonnet29() {
poem = { "foo", "bar" , "baz"};
}
@Override
public void recite() {
//...
}
}
Line poem = { "foo", "bar" , "baz"};is giving compilation error.
行poem = { "foo", "bar" , "baz"};给出编译错误。
Any specific reason why this is not allowed? How do I initialize a String array with array constants?
不允许这样做的任何具体原因?如何使用数组常量初始化字符串数组?
EDIT: Thank you folks for your answers. Now I'm clear what is allowed and what is NOT. But can I ask you whythis is NOT allowed?
编辑:谢谢大家的回答。现在我很清楚什么是允许的,什么是不允许的。但我能问你为什么不允许这样做吗?
String[] pets;
pets = {"cat", "dog"};
After googling a bit, I found this link, where in, it is told that coding like this leaves the compiler in ambiguity - whether the pets should be array of Strings or array of Objects. However from the declaration, it can very well figure out that it is a String array, right???
在谷歌上搜索了一下之后,我找到了这个链接,在那里,它被告知这样的编码会使编译器含糊不清 - 宠物应该是字符串数组还是对象数组。但是从声明中可以很好的判断出它是一个String数组,对吧???
回答by ZoogieZork
This will do what you're looking for:
这将执行您正在寻找的操作:
public Sonnet29() {
poem = new String[] { "foo", "bar", "baz" };
}
Initialization lists are only allowed when creating a new instance of the array.
仅在创建数组的新实例时才允许使用初始化列表。
回答by Andreas Dolk
From the Java language specification:
An array initializer may be specified in a declaration, or as part of an array creation expression (§15.10), creating an array and providing some initial values
数组初始值设定项可以在声明中指定,或作为数组创建表达式(第 15.10 节)的一部分,创建数组并提供一些初始值
In short, this is legal code:
简而言之,这是合法的代码:
private int[] values1 = new int[]{1,2,3,4};
private int[] values2 = {1,2,3,4}; // short form is allowed only (!) here
private String[][] map1 = new String[][]{{"1","one"},{"2","two"}};
private String[][] map2 = {{"1","one"},{"2","two"}}; // short form
List<String> list = Arrays.asList(new String[]{"cat","dog","mouse"});
and this is illegal:
这是非法的:
private int[] values = new int[4];
values = {1,2,3,4}; // not an array initializer -> compile error
List<String> list = Arrays.asList({"cat","dog","mouse"}); // 'short' form not allowed
回答by Ishtar
{"cat", "dog"}
Is not an array, it is an array initializer.
不是数组,它是一个数组初始值设定项。
new String[]{"cat", "dog"}
This can be seen as an array 'constructor' with two arguments. The short form is just there to reduce RSI.
这可以看作是一个带有两个参数的数组“构造函数”。简短形式只是为了减少 RSI。
They could have given real meaning to {"cat", "dog"}, so you could say things like
他们本可以赋予 {"cat", "dog"} 真正的意义,所以你可以这样说
{"cat", "dog"}.length
But why should they make the compiler even harder to write, without adding anything useful? (ZoogieZork answer can be used easily)
但是为什么他们要让编译器更难编写,而不添加任何有用的东西呢?(ZoogieZork 答案可以轻松使用)

