简体   繁体   English

可变参数(varargs)Java用法

[英]Variable aguments(varargs) java usage

I was reading a book about java and the author did some variable arguments . 我正在读一本关于Java的书,作者做了一些可变的论点 It is something just like this: 就像这样:

public int num(int ... nums){}

and I did some research it looks like nums is simply an array. 我做了一些研究,看起来nums只是一个数组。 So I am thinking the above code can be then replaced as: 所以我认为上面的代码可以替换为:

public int num(int[] nums){}

My question: What is the point of the variable arguments? 我的问题:变量参数的意义是什么? Can you change the type to other types such as String ? 您可以将类型更改为其他类型,例如String吗?

The difference would be in how you can call the method. 不同之处在于您如何调用该方法。

If your method looked like this: 如果您的方法如下所示:

public int num(int[] nums){}

you could call it like this: 您可以这样称呼它:

num(new int[] { 1, 2, 3 });

On the other hand, if you used varargs like this: 另一方面,如果您像这样使用varargs:

public int num(int ... nums){}

you could call it more concisely, like this: 您可以更简洁地调用它,如下所示:

num(1, 2, 3)

Varargs are just syntactic sugar that lets you write Varargs只是语法糖,可以让您编写

num(1,2,3);

instead of 代替

num(new int[] {1,2,3});
System.out.println("%s %s %s.", "this", "is", "why");

System.out.println("%s %s %s %s?", new String[] {"or", "you", "prefer", "this"});

Varargs (variable arguments) do indeed get packaged into an array when your program is run. 程序运行时,确实确实将Varargs (可变参数)打包到一个数组中。 There is no functional difference between making a call like this 这样拨打电话之间没有功能上的区别

public int add(int... addends);

and this 和这个

public int add(int[] addends);

Java will create an int[] for you and store it in the variable addends in the former. Java将为您创建一个int[]并将其存储在前者的变量addends中。 In the latter, you make it so the caller of the add function has to create the array themselves. 在后者中,您可以这样做,因此add函数的调用者必须自己创建数组。 This can be a bit ugly, so Java made it easy. 这可能有点丑陋,所以Java使它变得容易。 You call the varargs method like so: 您可以这样调用varargs方法:

int sum = add(1,2,3,4,5,6);

And you call the array method like so: 然后像这样调用数组方法:

int sum = add( new int[] {1,2,3,4,5,6} );

Varargs can be used for more than just primitives, as you suspected; 正如您所怀疑的,Varargs不仅可以用于基本类型。 you can use them for Objects (of any flavor) as well: 您也可以将它们用于对象(任何样式):

public Roster addStudents(Student... students) {}

A quote from the article linked: 文章引用语链接:

So when should you use varargs? 那么什么时候应该使用varargs? As a client, you should take advantage of them whenever the API offers them. 作为客户端,只要API提供了它们,就应该利用它们。 Important uses in core APIs include reflection, message formatting, and the new printf facility. 核心API的重要用途包括反射,消息格式和新的printf工具。 As an API designer, you should use them sparingly, only when the benefit is truly compelling. 作为API设计人员,只有真正受益匪浅时,才应谨慎使用它们。 Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called. 一般来说,您不应该重载varargs方法,否则程序员将很难弄清楚调用哪个重载。

Edit: Added example for using Objects 编辑:添加了使用对象的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM