Java 此方法签名中的省略号 (...) 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2367398/
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
What is the ellipsis (...) for in this method signature?
提问by natchy
In the App Engine docs, what is the ellipsis (JID...
) for in this method signature?
在App Engine 文档中,JID...
此方法签名中的省略号 ( ) 是什么?
public MessageBuilder withRecipientJids(JID... recipientJids)
What's the function of those three dots?
这三个点的作用是什么?
采纳答案by FrustratedWithFormsDesigner
Those are Java varargs. They let you pass any number of objects of a specific type (in this case they are of type JID).
这些是 Java 可变参数。它们允许您传递任意数量的特定类型的对象(在本例中它们是 JID 类型)。
In your example, the following function calls would be valid:
在您的示例中,以下函数调用是有效的:
MessageBuilder msgBuilder; //There should probably be a call to a constructor here ;)
MessageBuilder msgBuilder2;
msgBuilder.withRecipientJids(jid1, jid2);
msgBuilder2.withRecipientJids(jid1, jid2, jid78_a, someOtherJid);
See more here: http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html
在此处查看更多信息:http: //java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html
回答by Rob
It means that the method accepts a variable number of arguments("varargs") of type JID
. Within the method, recipientJids
is presented.
这意味着该方法接受类型为可变数量的参数(“varargs”)JID
。在该方法中,recipientJids
介绍了。
This is handy for cases where you've a method that can optionally handle more than one argument in a natural way, and allows you to write calls which can pass one, two or three parameters to the same method, without having the ugliness of creating an array on the fly.
这对于您有一个可以选择以自然方式处理多个参数的方法的情况很方便,并且允许您编写可以将一个、两个或三个参数传递给同一个方法的调用,而没有创建的丑陋动态数组。
It also enables idioms such as sprintf
from C; see String.format()
, for example.
它还支持诸如sprintf
来自 C 的习语;见String.format()
,例如。
回答by OscarRyz
Those are varargs
they are used to create a method that receive any number of arguments.
这些都是varargs
他们用来创建接收任意数量的参数的方法。
For instance PrintStream.printfmethod uses it, since you don't know how many would arguments you'll use.
例如PrintStream.printf方法使用它,因为您不知道将使用多少个参数。
They can only be used as final position of the arguments.
它们只能用作参数的最终位置。
varargs
was was added on Java 1.5
varargs
在Java 1.5上添加
回答by James Goodwin
The three dot (...) notation is actually borrowed from mathematics, and it means "...and so on".
三点(...)符号实际上是从数学中借来的,它的意思是“...等等”。
As for its use in Java, it stands for varargs
, meaning that any number of arguments can be added to the method call. The only limitations are that the varargs
must be at the end of the method signature and there can only be one per method.
至于它在 Java 中的使用,它代表varargs
,这意味着可以将任意数量的参数添加到方法调用中。唯一的限制是varargs
必须在方法签名的末尾,并且每个方法只能有一个。
回答by Lucio
The way to use the ellipsisor varargsinside the method is as if it were an array:
在方法中使用省略号或可变参数的方式就像是一个数组:
public void PrintWithEllipsis(String...setOfStrings) {
for (String s : setOfStrings)
System.out.println(s);
}
This method can be called as following:
这个方法可以调用如下:
obj.PrintWithEllipsis(); // prints nothing
obj.PrintWithEllipsis("first"); // prints "first"
obj.PrintWithEllipsis("first", "second"); // prints "first\nsecond"
Inside PrintWithEllipsis
, the type of setOfStrings
is an array of String.
So you could save the compiler some work and pass an array:
在 里面PrintWithEllipsis
, 的类型setOfStrings
是一个字符串数组。所以你可以节省编译器的一些工作并传递一个数组:
String[] argsVar = {"first", "second"};
obj.PrintWithEllipsis(argsVar);
For varargsmethods, a sequence parameter is treated as being an array of the same type. So if two signatures differ only in that one declares a sequence and the other an array, as in this example:
对于可变参数方法,序列参数被视为相同类型的数组。因此,如果两个签名的不同之处仅在于一个声明了一个序列,另一个声明了一个数组,如本例所示:
void process(String[] s){}
void process(String...s){}
then a compile-time error occurs.
然后发生编译时错误。
Source: The Java Programming Languagespecification, where the technical term is variable arity parameter
rather than the common term varargs
.
来源:Java 编程语言规范,其中技术术语variable arity parameter
而不是通用术语varargs
。