Java 数组参数声明语法“...”是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4211099/
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
How does the Java array argument declaration syntax "..." work?
提问by Virtually Real
I have been writing java for a while, and today I encountered the following declaration:
写java有一段时间了,今天遇到如下声明:
public static void main(String... args) {
}
Note the "dot dot dot" in the array declaration, rather than the usual bracket []. Clearly it works. In fact I wrote a small test and verified it works. So, I pulled the java grammar to see where this syntax of argument declaration is, but did not find anything.
注意数组声明中的“dot dot dot”,而不是通常的括号 []。显然它有效。事实上,我写了一个小测试并验证了它的工作原理。所以,我把java语法拉出来看看这个参数声明的语法在哪里,但没有找到任何东西。
So to the experts out there, how does this work? Is it part of the grammar? Also, while I can declare function like this, I can't declare an array within a function's body like this.
那么对于那里的专家来说,这是如何工作的?它是语法的一部分吗?此外,虽然我可以像这样声明函数,但我不能像这样在函数体内声明数组。
Anyway, do you know of any place that has this documented. It is curiosity, and perhaps not worth of any time invested in it, but I was stumped.
不管怎样,你知道任何有这个记录的地方吗?这是好奇心,也许不值得投入任何时间,但我被难住了。
采纳答案by hvgotcodes
Check out the Java Language Specification, Third Edition, Chapter 8 (Classes). Buried in there is this nugget:
查看Java 语言规范,第三版,第 8 章(类)。埋在那里的是这个金块:
If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (§15.12.4.2).
如果最后一个形参是 T 类型的可变元参数,则认为定义了一个 T[] 类型的形参。该方法则是可变元方法。否则,它是一个固定的数量方法。可变参数方法的调用可能包含比形式参数更多的实际参数表达式。所有与变量 arity 参数之前的形式参数不对应的实际参数表达式都将被评估,并将结果存储到一个数组中,该数组将传递给方法调用(第 15.12.4.2 节)。
Basically, the last parameter of any method call can have T...
. If it has that, it is converted to T[]
.
基本上,任何方法调用的最后一个参数都可以有T...
. 如果有,则将其转换为T[]
.
So basically, what you have is a fancy way of reproducing the more traditional
所以基本上,你拥有的是一种复制更传统的奇特方式
String[] args
回答by Ming-Tang
It's called varadic argument: A function that takes as many (including zero) arguments as you want.
For example, main("string1", "string2", "string3")
is same as main({"string1", "string2", "string3"})
if main is declared as void main(String...args)
.
它被称为 varadic 参数:一个函数,可以根据需要接受任意数量(包括零)个参数。例如,main("string1", "string2", "string3")
与将main({"string1", "string2", "string3"})
main 声明为void main(String...args)
.
See http://www.java-tips.org/blog/java-se/varargs-%E2%80%93-java-50-addition.html
见http://www.java-tips.org/blog/java-se/varargs-%E2%80%93-java-50-addition.html
回答by icyrock.com
This is called variadic arguments, check here:
这称为可变参数,请在此处查看:
The official documentation (Java 1.5) is here:
官方文档(Java 1.5)在这里:
回答by JohnS
I believe this was implemented in Java 1.5. The syntax allows you to call a method with a comma separated list of arguments instead of an array.
我相信这是在 Java 1.5 中实现的。该语法允许您使用逗号分隔的参数列表而不是数组来调用方法。
public static void main(String... args);
main("this", "is", "multiple", "strings");
is the same as:
是相同的:
public static void main(String[] args);
main(new String[] {"this", "is", "multiple", "strings"});
http://today.java.net/article/2004/04/13/java-tech-using-variable-argumentshttp://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
http://today.java.net/article/2004/04/13/java-tech-using-variable-arguments http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs .html
回答by Jigar Joshi
It is varargs
这是 varargs
In simple term its an Array
of Member
like
在简单的短期的一个Array
的Member
像
public setMembers(Member[] members);
When to use:
何时使用:
Generally while designing API it is good to use when number of argument is not fixed.
通常在设计 API 时,最好在参数数量不固定的情况下使用。
Example from standard API of this is String.format(String format,Object... args)
来自标准 API 的示例是 String.format(String format,Object... args)
Also See
另见
回答by Brian Topping
It means you can pass zero or more Member
objects to the setMembers()
method. In the setMembers method, members
will have array semantics.
这意味着您可以将零个或多个Member
对象传递给该setMembers()
方法。在 setMembers 方法中,members
将具有数组语义。
回答by Justin Niessner
You might want to read up on Using Variable Arguments (or varargs) in Java.
您可能想阅读在 Java 中使用变量参数(或可变参数)。
回答by fwielstra
It's the so-called varargs syntax. In the method body, you can read the members
parameter as it were an array - actually, it /is/ 'just' an array.
这就是所谓的可变参数语法。在方法主体中,您可以读取members
参数,因为它是一个数组 - 实际上,它 /is/“只是”一个数组。
However, the magic bit is in calling the method. Before the varargs
syntax was introduced, you'd call the method a bit like so:
然而,神奇的地方在于调用方法。在varargs
引入语法之前,您可以像这样调用该方法:
setMembers(new Members[] {member1, member2, member3});
With the new varargs syntax however, you don't need to explicitly create the array anymore, and you can pass:
但是,使用新的 varargs 语法,您不再需要显式创建数组,您可以通过:
setMembers(member1, member2, member3);
This does mean however that a varargs
argument has to be the last argument in a method. Something like this is therefore not permitted:
然而,这确实意味着varargs
参数必须是方法中的最后一个参数。因此,不允许这样的事情:
void setMembers(Member ... members, String memberType);
Summarized: It's a bit of syntactic sugar, really. I'm no expert on the inner workings of the Java compiler, but I'm pretty sure that methods calling a method that accept a varargs
parameter are rebuilt into methods that build an array of the given type.
总结:真的,这有点语法糖。我不是 Java 编译器内部工作方面的专家,但我很确定调用接受varargs
参数的方法的方法会重新构建为构建给定类型数组的方法。
回答by Jan Zyka
Variable arguments. Can have 0 or more String arguments.
可变参数。可以有 0 个或多个 String 参数。
The function can access the parameters as an array of Strings.
该函数可以以字符串数组的形式访问参数。
回答by dj18
It means that the method accepts a variable number of String arguments. The arguments are treated as an array and so are accessed by subscript, in the order they are passed in.
这意味着该方法接受可变数量的字符串参数。参数被视为一个数组,因此按它们传入的顺序通过下标访问。