Java “字符串……”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18176534/
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 does 'String...' mean?
提问by David Silva
In the code:
在代码中:
public interface ProductInterface {
public List<ProductVO> getProductPricing(ProductVO product, ProductVO prodPackage, String... pricingTypes) throws ServiceException;
}
What does
有什么作用
String... pricingTypes
mean? What type of construct is this?
意思?这是什么类型的构造?
回答by Brad Peabody
It's a vararg - variable argument. You can pass a value of that type as many times as you want and the caller gets it as an array.
这是一个 vararg - 可变参数。您可以根据需要多次传递该类型的值,调用者将其作为数组获取。
http://docs.oracle.com/javase/7/docs/technotes/guides/language/varargs.html
http://docs.oracle.com/javase/7/docs/technotes/guides/language/varargs.html
回答by nanofarad
It is called varargs. It works for any type as long as it's the lastargument in the signature.
它被称为可变参数。只要它是签名中的最后一个参数,它就适用于任何类型。
Basically, any number of parameters are put into an array. This does not mean that it is equivalent to an array.
基本上,任意数量的参数都被放入一个数组中。这并不意味着它等同于一个数组。
A method that looks like:
一个看起来像的方法:
void foo(int bar, Socket baz...)
will have an arrayof Socket (in this example) called baz.
将有一个名为 baz 的 Socket数组(在本例中)。
So, if we call foo(32, sSock.accept(), new Socket())
we'll find an array with two Socket objects.
因此,如果我们调用,foo(32, sSock.accept(), new Socket())
我们将找到一个包含两个 Socket 对象的数组。
Calling it as foo(32, mySocketArray)
will notwork as the type is notconfigured to take an array. However, if the signature is a varargs of arrays you can pass one or more arrays and get a two-dimensional array. For example, void bar(int bar, PrintStream[] baz...)
can take multiple arrays of PrintStream and stick them into a single PrintStream[][]
.
调用它作为foo(32, mySocketArray)
将不作为类型时工作不被配置为采取的阵列。但是,如果签名是数组的可变参数,您可以传递一个或多个数组并获得二维数组。例如,void bar(int bar, PrintStream[] baz...)
可以采用多个 PrintStream 数组并将它们粘贴到单个PrintStream[][]
.
Oddly enough, due to the fact that arrays are objects, Object... foo
can take any number of arrays.
奇怪的是,由于数组是对象,因此Object... foo
可以采用任意数量的数组。