为什么将 {a, b, c} 传递给方法不起作用?
我尝试将初始化列表 {...} 传递给构造函数,但它不起作用。 当我在方法局部变量(int[])中声明它时,它工作得完美无缺。
这是为什么?
public class QuickSort {
int[] a;
public QuickSort(int[] a) {
this.a = a;
}
public static void main(String[] args) {
// ###################
// ### WORKS ##
// ###################
int[] a = {8,12,79,12,50,44,8,0,7,289,1};
QuickSort sort = new QuickSort(a);
// ###################
// ### DOESN'T WORK ##
// ###################
//QuickSort sort = new QuickSort({8,12,79,12,50,44,8,0,7,289,1});
}
}
I've tried to pass an initialization list {...} to a constructor and it didn't work.
When I instead declared it in a method local variable (int[]) it worked flawlessly.
Why is that?
public class QuickSort {
int[] a;
public QuickSort(int[] a) {
this.a = a;
}
public static void main(String[] args) {
// ###################
// ### WORKS ##
// ###################
int[] a = {8,12,79,12,50,44,8,0,7,289,1};
QuickSort sort = new QuickSort(a);
// ###################
// ### DOESN'T WORK ##
// ###################
//QuickSort sort = new QuickSort({8,12,79,12,50,44,8,0,7,289,1});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当声明
int[]
并分配{1, 2, 3}
时,编译器知道您想要创建一个int[]
,因为它的拼写就在那里。在后一种情况下,您将数组直接粘贴到方法调用中,您必须使用它
来告诉编译器您的数组是什么。
When declaring an
int[]
and assigning{1, 2, 3}
the compiler knows you want to create anint[]
as it's spelled out right there.In the latter case where you stick the array directly into the method call you would have to use
to tell the compiler what your array is.
{}
构造称为数组初始值设定项,它用于在 Java 中初始化数组。 (参考:第 10.6 节:数组初始化器 Java 语言规范,第三版。)原因为什么传递
{1, 2, 3}
本身无效是因为没有与初始值设定项关联的类型信息。因此,必须通过编写
new Type[]
让编译器知道数组的类型,其中Type
是数组所针对的类型。以下都是数组初始值设定项的有效用法:
new String[] {"Hello, "World"}
new Character[] {'A', 'B'}
new Runnable[] {new Runnable() {public void run() {}}, new Runnable() {public void run() {}}
可以看出,该表示法可用于许多数据类型,所以它不是特定于整数的。
至于:
上面有效的原因是因为类型信息是在变量类型声明中提供给编译器的,在本例中是 int[。 ] 上面的含义如下:
现在,如果我们有
new int[] {1, 2, 3}
,我们就可以创建一个新的int []
数组就位,因此可以像任何其他int[]
数组一样处理——只是它没有与之关联的变量名称,因此,由
new int[] {1, 2, 3}
创建的数组可以发送到以int[]
作为参数的方法或构造函数中:The
{}
construct is called an array initializer, and it is used to initialize an array in Java. (Reference: Section 10.6: Array Initializers from The Java Language Specification, Third Edition.)The reason why passing
{1, 2, 3}
itself is not valid is because there is no type information associated with the initializer.Therefore, one must let the compiler know the type of the array is by writing
new Type[]
, where theType
is the type for which the array is made for.The following are all valid use of the array initializer:
new String[] {"Hello, "World"}
new Character[] {'A', 'B'}
new Runnable[] {new Runnable() {public void run() {}}, new Runnable() {public void run() {}}
As can be seen, this notation can be used for many data types, so it's not something that is specific for integers.
As for:
The reason why the above is valid is because the type information is provided to the compiler in the variable type declaration, which in this case is
int[]
. What the above is implying is the following:Now, if we have
new int[] {1, 2, 3}
, we are able to create a newint[]
array in place, so that can be handled as any otherint[]
array would -- it's just that it doesn't have a variable name associated with it.Therefore, the array created by
new int[] {1, 2, 3}
can be sent into an method or constructor that takes aint[]
as its argument:这可能是因为您的初始化列表没有键入信息。
尝试这个:
This is probably because your initialization list has no typing information to it.
Try this:
你也可以这样做:
You also can do like this:
Java 并没有真正的类型推断。 数组变量声明是特殊情况 Java 语言规范,一种不适用于方法参数的规范。 这样做是可能的,但会给规范增加很多复杂性,因为它必须处理诸如 {"a", "b"} 是否创建 String[] 还是 Object[] 之类的问题 - 在这里看起来很明显,但如果它是复杂类型层次结构中的对象怎么办? 如果该方法被重载并且两个版本都存在怎么办?
Java doesn't really have type inference. Array variable declarations are a special case in the Java language specification, one that does not apply to method parameters. Doing so would be possible, but would add a lot of complexity to the spec, since it would have to handle questions like whether {"a", "b"} creates a String[] or an Object[] - looks obvious here, but what if it's objects in a complex type hierarchy? And what if the method is overloaded and both versions exist?
大括号(当在数组文字中使用时)只能在声明数组时使用:)
Curly braces (when used in array literals) may only be used when declaring the array :)
在将列表传递给构造函数之前,您是否尝试过将列表强制转换为 int[] ?
Have you tried casting the list to int[] before passing it to the constructor?