Java varargs 和 '...' 参数

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

varargs and the '...' argument

javaarraysparametersvariadic-functions

提问by Martijn Courteaux

Consider the method declaration:

考虑方法声明:

String.format(String, Object ...)

The Object ...argument is just a reference to an array of Objects. Is there a way to use this method with a reference to an actual Objectarray?If I pass in an Objectarray to the ...argument - will the resultant argument value be a two-dimensional array - because an Object[]is itself an Object:

Object ...参数是只是阵列的参考Object秒。有没有办法通过引用实际Object数组来使用此方法?如果我将Object数组传递给...参数 - 结果参数值是否是二维数组 - 因为 anObject[]本身就是 an Object

Object[] params = ....; // Make the array (for example based on user-input)
String s = String.format("%S has %.2f euros", params);

So the first component of the array (Which is used in the String.formatmethod), will be an array and he will generate:

所以数组的第一个组件(在String.format方法中使用)将是一个数组,他将生成:

[class.getName() + "@" + Integer.toHexString(hashCode())] 

and then an error because the array size is 1.

然后因为数组大小为 1 而出现错误。

The boldsequence is the real question.
This is a second question: Does a ...array/parameter have a name?

大胆的顺序是真正的问题。
这是第二个问题:是否一个...数组/参数有名字吗?

采纳答案by Ayman Hourieh

From the docs on varargs:

关于 varargs文档

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.

最后一个参数类型后的三个句点表示最后一个参数可以作为数组或参数序列传递。

So you can pass multiple arguments or an array.

所以你可以传递多个参数或一个数组。

The following works just fine:

以下工作正常:

class VarargTest {
  public static void main(String[] args) {
    Object[] params = {"x", 1.2345f};
    String s = String.format("%s is %.2f", params);
    System.out.println(s); // Output is: x is 1.23
  }
}

回答by cletus

You can just pass an array:

你可以只传递一个数组:

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

String args[] = new String[10];
foo(args);

回答by mcherm

The situation you are describing is going to be fairly rare: most of the time, your varargs items will be Strings, or numbers, or Widgets... it will be unusual for them to be Objects (which could be anything) or arrays.

您所描述的情况将非常罕见:大多数情况下,您的可变参数项将是Strings、数字或Widgets……它们是Objects(可以是任何东西)或数组是不寻常的。

But if the varargs argument isa bunch of Objects or an array type, then your question does arise: you can pass it a single array and then how will the compiler know whether you meant to pass an array (the one you provided), or an series of 1 item which it should PUT into an array for you?

但是,如果 varargs 参数一堆Objects 或数组类型,那么您的问题确实会出现:您可以将一个数组传递给它,然后编译器如何知道您是否打算传递一个数组(您提供的数组),或者它应该为您放入数组的一系列 1 项?

A quick test shows the answer:

一个快速测试显示了答案:

public class TestClass {
    public static void main(String[] args) {
        Object anObject = new Object();
        Object[] anArray = new Object[] {anObject, anObject};
        System.out.println("object1 = " + anObject);
        System.out.println("array1 = " + anArray);
        takesArgs();
        takesArgs(anObject, anObject);
        takesArgs(anArray); // is this the same as array1?
        takesArgs(anArray, anArray);
    }

    public static void takesArgs(Object... stuff) {
        System.out.println("The array was " + stuff);
    }
}

The result of executing (your exact numbers will vary:

执行的结果(您的确切数字会有所不同:

object1 = java.lang.Object@3e25a5
array1 = [Ljava.lang.Object;@19821f
The array was [Ljava.lang.Object;@addbf1
The array was [Ljava.lang.Object;@42e816
The array was [Ljava.lang.Object;@19821f
The array was [Ljava.lang.Object;@9304b1

So the answer is that in ambiguous cases it treats what you passed as the array instead of creating a new array to wrap it. This makes sense as you could always wrap it in an array yourself if you wanted the other interpretation.

所以答案是,在不明确的情况下,它会将您传递的内容视为数组,而不是创建一个新数组来包装它。这是有道理的,因为如果您想要其他解释,您总是可以自己将它包装在一个数组中。