Java可变长度参数与数组,只是语法糖? [英] Java Variable Length Parameter vs. Array, Simply Syntactic Sugar?

查看:540
本文介绍了Java可变长度参数与数组,只是语法糖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一家本地社区大学参加数据结构和算法"课程的学习.该课程的教科书是Y. Daniel Liang的 Java编程简介,第10版.这本书本身很扎实.

I am taking a Data Structures and Algorithms course for fun at a local community college. The course's textbook is Y. Daniel Liang's Introduction to Java Programming, 10th Edition. The book, itself, is pretty solid.

在处理Java.util.Arrays时,Liang提到了Java的可变长度"参数.他写道(第265页):

In dealing with Java.util.Arrays, Liang mentions Java's "variable-length" parameter. He writes (p. 265):

Java将可变长度参数视为数组.您可以通过 数组或可变长度的可变数量的参数 范围.当调用具有可变数量的参数的方法时, Java创建一个数组并将参数传递给它.

Java treats a variable-length parameter as an array. You can pass an array or a variable number of arguments to a variable-length parameter. When invoking a method with a variable number of arguments, Java creates an array and passes the arguments to it.

一个例子是:

public static void (int... toes) {
    //... some code
}

但是,Liang从未解释可变长度参数的起源或优势.如Liang所说,如果将可变长度参数转换"为数组,那么它们的优点是什么?可变长度参数是否有助于实现软件设计模式或工程目标?

However, Liang never explains the origin or advantage of the variable-length parameter. If, as Liang says, the variable-length parameter is "converted" to an array, what is their advantage? Is there a software design pattern or engineering goal that is facilitated by the variable length parameter?

换句话说,上面的代码段提供了以下内容所没有的内容:

In other words, what does the above code snippet offer that is not offered by the below:

public static void (int[] toes) { // ...

推荐答案

在Java中,varargs是在调用方法时用于创建数组的语法糖.例如,这两个调用是等效的:

In Java, varargs are a syntactical sugar for creating an array when calling a method. For example, these two calls are equivalent:

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

foo("hello", null, "world", xyz);  // Java 1.5+
foo(new String[]{"hello", null, "world", xyz});  // All versions of Java

Varargs并没有使任何新事物成为可能(根据句法糖的定义),但是它减少了冗长性,并使某些构造更易于实现.例如,我最喜欢的vararg用法包括:

Varargs doesn't make anything new possible (by definition of syntactic sugar), but it reduces verboseness and makes some constructs much more agreeable to implement. For example, some of my favorite uses of vararg include: PrintStream.printf(), String.format(), Method.invoke().

varargs的其他良好应用程序:

Other good applications of varargs:

// This one is in the Java standard library
Collections: void addAll(Collection<? super T> c, T... elements);

// Custom examples
int max(int... nums);
void doOperation(File x, String y, SomeEnum options...);

此外,Java的varargs使该语言与C,Python,JavaScript和其他语言中的vararg支持达到同等地位.因此,如果经常使用的设计(例如max())最适合使用varargs,则Java不再是需要较丑陋的实现的奇怪语言.

Additionally, Java's varargs bring the language up to parity with the vararg support in C, Python, JavaScript, and other languages. So if a frequently recurring design (such as max()) works best with varargs, Java is no longer the odd language that requires an uglier implementation.

这篇关于Java可变长度参数与数组,只是语法糖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆