Java 参数列表中的“...”是什么意思?doInBackground(字符串...参数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3960236/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:37:37  来源:igfitidea点击:

What does the "..." mean in a parameter list? doInBackground(String... params)

javavariadic-functions

提问by Jim

I don't understand that syntax. Trying to google various words plus "..." is useless.

我不明白那个语法。试图用谷歌搜索各种单词加上“...”是没有用的。

采纳答案by Margus

This is called Variadic function(wiki page with examples in many languges).

这称为Variadic 函数包含多种语言示例的 wiki 页面)。

In computer programming, a variadic function is a function of indefinite arity, i.e. one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. There are many mathematical and logical operations that come across naturally as variadic functions. For instance, the summing of numbers or the concatenation of strings or other sequences are operations that can logically apply to any number of operands. Another operation that has been implemented as a variadic function in many languages is output formatting. The C function printf and the Common Lisp function format are two such examples. Both take one argument that specifies the formatting of the output, and any number of arguments that provide the values to be formatted. Variadic functions can expose type-safety problems in some languages. For instance, C's printf, if used incautiously, can give rise to a class of security holes known as format string attacks. The attack is possible because the language support for variadic functions is not type-safe; it permits the function to attempt to pop more arguments off the stack than were placed there -- corrupting the stack and leading to unexpected behavior. Variadic functionality can be considered complementary to the apply function, which takes a function and a list/sequence/array as arguments and then calls the function once, with the arguments being the elements of the list.

在计算机编程中,可变参数函数是一个不定元函数,即接受可变数量参数的函数。不同编程语言对可变参数函数的支持差异很大。有许多数学和逻辑运算可以自然地作为可变参数函数出现。例如,数字求和或字符串或其他序列的连接是逻辑上可以应用于任何数量的操作数的操作。在许多语言中作为可变参数函数实现的另一个操作是输出格式化。C 函数 printf 和 Common Lisp 函数格式就是两个这样的例子。两者都采用一个指定输出格式的参数,以及提供要格式化的值的任意数量的参数。可变参数函数可能会暴露某些语言中的类型安全问题。例如,C 的 printf 如果使用不当,可能会产生一类称为格式字符串攻击的安全漏洞。攻击是可能的,因为对可变参数函数的语言支持不是类型安全的;它允许函数尝试从堆栈中弹出比放置在堆栈中更多的参数——破坏堆栈并导致意外行为。可变参数功能可以被认为是对 apply 函数的补充,它接受一个函数和一个列表/序列/数组作为参数,然后调用该函数一次,参数是列表的元素。可能会产生一类称为格式字符串攻击的安全漏洞。攻击是可能的,因为对可变参数函数的语言支持不是类型安全的;它允许函数尝试从堆栈中弹出比放置在堆栈中更多的参数——破坏堆栈并导致意外行为。可变参数功能可以被认为是对 apply 函数的补充,它接受一个函数和一个列表/序列/数组作为参数,然后调用该函数一次,参数是列表的元素。可能会产生一类称为格式字符串攻击的安全漏洞。攻击是可能的,因为对可变参数函数的语言支持不是类型安全的;它允许函数尝试从堆栈中弹出比放置在堆栈中更多的参数——破坏堆栈并导致意外行为。可变参数功能可以被认为是对 apply 函数的补充,它接受一个函数和一个列表/序列/数组作为参数,然后调用该函数一次,参数是列表的元素。它允许函数尝试从堆栈中弹出比放置在堆栈中更多的参数——破坏堆栈并导致意外行为。可变参数功能可以被认为是对 apply 函数的补充,它接受一个函数和一个列表/序列/数组作为参数,然后调用该函数一次,参数是列表的元素。它允许函数尝试从堆栈中弹出比放置在堆栈中更多的参数——破坏堆栈并导致意外行为。可变参数功能可以被认为是对 apply 函数的补充,它接受一个函数和一个列表/序列/数组作为参数,然后调用该函数一次,参数是列表的元素。

One of may personal favoritenot used features in Java. It is basically a reference array that is built from elements. One of the best ways to use it is on class constructor, or method where you need to constantly find a value like maximum of 2, 3, 4, 5 input elements.

一个可以个人最喜欢在不使用的功能的Java。它基本上是一个由元素构建的引用数组。使用它的最佳方法之一是在类构造函数或方法上,您需要不断找到一个值,例如最多 2、3、4、5 个输入元素。

One example is, when i built a generic binary tree node, for coding tasks, I used this in constructor. This enabled me simply add elements to the tree and distribute them.

一个例子是,当我binary tree node为编码任务构建泛型时,我在构造函数中使用了它。这使我能够简单地将元素添加到树中并分发它们。

Following creates String type binary tree, with root "Red"and 2 branches "Blue"and "Green".

以下创建字符串类型的二叉树,具有根"Red"和 2 个分支"Blue""Green".

new MBTN<String>("Red", "Blue", "Green").

Could you think what the alternatives would be :D You can't even simply make generic array of elements, so this would stretch like hell. It is definitely not useless.

你能想到替代方案是什么吗:D 你甚至不能简单地制作元素的通用数组,所以这会像地狱一样伸展。绝对不是没用。

回答by BalusC

It's called varargs. This fact should yield better Google results.

它被称为varargs。这个事实应该会产生更好的谷歌结果

回答by Manuel Selva

It's a java method that took an arbitrary variable number of parameters

这是一个采用任意可变数量参数的java方法

回答by Andy

They are variable length parameters.

它们是可变长度参数。

Here is one link with an example.

这是一个带有示例的链接

回答by Mike Deck

As @BalusC mentioned, it's a varags parameter. This means you can pass a variable number of arguments to that method.

正如@BalusC 所提到的,它是一个 varags 参数。这意味着您可以将可变数量的参数传递给该方法。

So for a method defined as

所以对于定义为的方法

public void foo(String...strings) {  }

The following invocations are legal:

以下调用是合法的:

foo();
foo("one param");
foo("one", "two", "three");

回答by Mads Lee Jensen

It means "Arbitrary number of arguments" meaning you can pass a unknown number of parameters into the method..

这意味着“任意数量的参数”意味着您可以将未知数量的参数传递到方法中。

see

http://download.oracle.com/javase/tutorial/java/javaOO/arguments.htmlLook for the "Arbitrary number of arguments" section

http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html查找“任意数量的参数”部分

public Polygon polygonFrom(Point... corners) {
    int numberOfSides = corners.length;
    double squareOfSide1, lengthOfSide1;
    squareOfSide1 = (corners[1].x - corners[0].x)*(corners[1].x - corners[0].x) 
            + (corners[1].y - corners[0].y)*(corners[1].y - corners[0].y) ;
    lengthOfSide1 = Math.sqrt(squareOfSide1);
    // more method body code follows that creates 
    // and returns a polygon connecting the Points
}

回答by Buhake Sindi

As mentioned by everyone...variable arguments (or varargs) allows you to do this....

正如每个人所提到的......变量参数(或可变参数)允许你这样做......

//Method we're using for varargs
public void doSomething(String... datas) {
    if (datas == null || datas.length == 0) {
        System.out.println("We got nothing");
    } else {
        for (String data: datas) {
            System.out.println(data);
        }
    }
}

Therefore, all these calls mentioned below are valid....

因此,下面提到的所有这些调用都是有效的......

String d[] = {"1", "2", "3"};
doSomething(d); //An array of String, as long as the type is exactly as the varargs type.

//OR
doSomething("1", "2", "3", "4"); //I can add "infinitely" many arguments as the JVM can allocate me....

//OR 
doSomething("1"); //Exactly 1;

Internally varargs is "essentially" a reference array of it's declared type.

内部可变参数“本质上”是其声明类型的引用数组。

回答by Edwin Buck

They are the "variable arguments" or varargs (for short).

它们是“可变参数”或可变参数(简称)。

Basically it allows the passing of an unspecified number of Strings, so the method signature

基本上它允许传递未指定数量的字符串,因此方法签名

public void printStuff(String...messages)

Effectively can handle the following calls

可以有效处理以下调用

printStuff("hi");
printStuff("hi", "bye");
printStuff("Hello", "How are you?", "I'm doing fine.", "See you later");

You can effectively consider this a type of autoboxing. The printStuffargument can be seen as an array, so printStuff(String...messages)is conceptually handled like printStuff(String[] messages). Wtih the calls above effectively acting like

您可以有效地将其视为一种自动装箱。所述printStuff参数可以被看作是一个数组,所以printStuff(String...messages)在概念上类似的处理printStuff(String[] messages)。上面的调用有效地像

printStuff(new String[] {"hi"});
printStuff(new String[] {"hi", "bye"});
printStuff(new String[] {"Hello", "How are you?", "I'm doing fine.", "See you later"});

To access the messages internally, you use typical Listhandling primitives. Something like

要在内部访问消息,您可以使用典型的List处理原语。就像是

...
if (messages != null) {
  for (String message : messages) {
    System.out.println(message);
  }
}
...

That there is no need to actually create arrays is a bit of syntactic sugar added to Java with the advent of auto boxing.

随着自动装箱的出现,不需要实际创建数组是 Java 中添加的一些语法糖。