String ... Groovy Notation代表什么? [重复]

时间:2022-05-18 17:20:19

This question already has an answer here:

这个问题在这里已有答案:

What does the String... of the following function means?

以下函数的String ...是什么意思?

public UpdateSettingsRequest(String... indices) {
    this.indices = indices;
}

1 个解决方案

#1


3  

It is called varargs. It works for any type as long as it's the last argument in the signature.

它被称为varargs。它适用于任何类型,只要它是签名中的最后一个参数。

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 array of 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 not work as the type is not configured 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可以使用任意数量的数组。

#1


3  

It is called varargs. It works for any type as long as it's the last argument in the signature.

它被称为varargs。它适用于任何类型,只要它是签名中的最后一个参数。

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 array of 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 not work as the type is not configured 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可以使用任意数量的数组。