Java varargs 和 '...' 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1656901/
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
varargs and the '...' argument
提问by Martijn Courteaux
Consider the method declaration:
考虑方法声明:
String.format(String, Object ...)
The Object ...
argument is just a reference to an array of Object
s. Is there a way to use this method with a reference to an actual Object
array?If I pass in an Object
array to the ...
argument - will the resultant argument value be a two-dimensional array - because an Object[]
is itself an Object
:
该Object ...
参数是只是阵列的参考Object
秒。有没有办法通过引用实际Object
数组来使用此方法?如果我将Object
数组传递给...
参数 - 结果参数值是否是二维数组 - 因为 anObject[]
本身就是 an Object
:
Object[] params = ....; // Make the array (for example based on user-input)
String s = String.format("%S has %.2f euros", params);
So the first component of the array (Which is used in the String.format
method), will be an array and he will generate:
所以数组的第一个组件(在String.format
方法中使用)将是一个数组,他将生成:
[class.getName() + "@" + Integer.toHexString(hashCode())]
and then an error because the array size is 1.
然后因为数组大小为 1 而出现错误。
The boldsequence is the real question.
This is a second question: Does a ...
array/parameter have a name?
该大胆的顺序是真正的问题。
这是第二个问题:是否一个...
数组/参数有名字吗?
采纳答案by Ayman Hourieh
From the docs on varargs:
从关于 varargs的文档:
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.
最后一个参数类型后的三个句点表示最后一个参数可以作为数组或参数序列传递。
So you can pass multiple arguments or an array.
所以你可以传递多个参数或一个数组。
The following works just fine:
以下工作正常:
class VarargTest {
public static void main(String[] args) {
Object[] params = {"x", 1.2345f};
String s = String.format("%s is %.2f", params);
System.out.println(s); // Output is: x is 1.23
}
}
回答by cletus
You can just pass an array:
你可以只传递一个数组:
public void foo(String... args) {
}
String args[] = new String[10];
foo(args);
回答by mcherm
The situation you are describing is going to be fairly rare: most of the time, your varargs items will be String
s, or numbers, or Widget
s... it will be unusual for them to be Object
s (which could be anything) or arrays.
您所描述的情况将非常罕见:大多数情况下,您的可变参数项将是String
s、数字或Widget
s……它们是Object
s(可以是任何东西)或数组是不寻常的。
But if the varargs argument isa bunch of Object
s or an array type, then your question does arise: you can pass it a single array and then how will the compiler know whether you meant to pass an array (the one you provided), or an series of 1 item which it should PUT into an array for you?
但是,如果 varargs 参数是一堆Object
s 或数组类型,那么您的问题确实会出现:您可以将一个数组传递给它,然后编译器如何知道您是否打算传递一个数组(您提供的数组),或者它应该为您放入数组的一系列 1 项?
A quick test shows the answer:
一个快速测试显示了答案:
public class TestClass {
public static void main(String[] args) {
Object anObject = new Object();
Object[] anArray = new Object[] {anObject, anObject};
System.out.println("object1 = " + anObject);
System.out.println("array1 = " + anArray);
takesArgs();
takesArgs(anObject, anObject);
takesArgs(anArray); // is this the same as array1?
takesArgs(anArray, anArray);
}
public static void takesArgs(Object... stuff) {
System.out.println("The array was " + stuff);
}
}
The result of executing (your exact numbers will vary:
执行的结果(您的确切数字会有所不同:
object1 = java.lang.Object@3e25a5
array1 = [Ljava.lang.Object;@19821f
The array was [Ljava.lang.Object;@addbf1
The array was [Ljava.lang.Object;@42e816
The array was [Ljava.lang.Object;@19821f
The array was [Ljava.lang.Object;@9304b1
So the answer is that in ambiguous cases it treats what you passed as the array instead of creating a new array to wrap it. This makes sense as you could always wrap it in an array yourself if you wanted the other interpretation.
所以答案是,在不明确的情况下,它会将您传递的内容视为数组,而不是创建一个新数组来包装它。这是有道理的,因为如果您想要其他解释,您总是可以自己将它包装在一个数组中。