带有“...”数组的Java主要方法? [重复]

时间:2022-06-06 16:22:36

Possible Duplicate:
What is the ellipsis (…) for in this method signature?
java: how can i create a function that supports any number of parameters?

可能重复:此方法签名中的省略号(...)是什么? java:我怎样才能创建一个支持任意数量参数的函数?

well i'm trying to figure out some examples and I found this kind of array definition for arguments in the main method. What's so special about this "..." and what's the difference between a normal String[] args?

好吧,我想弄清楚一些例子,我在main方法中找到了这种参数的数组定义。这个“......”有什么特别的,普通的String [] args之间有什么区别?

Thanks

4 个解决方案

#1


7  

That's a notation from Java 5 for variable length argument lists. It is roughly equivalent to a String array, but lets you pass individual parameters that are combined into an array automatically.

这是Java 5中用于可变长度参数列表的符号。它大致相当于String数组,但允许您自动传递组合成数组的各个参数。

void mymethod(String... a) {
    for (String s : a) {
        ...
    }
}

mymethod("quick", "brown", "fox");

This makes sense only when you plan to call a method from your program, so I do not see why it would be desirable to use this syntax for your main(). It will work, however.

这只有在您计划从程序中调用方法时才有意义,因此我不明白为什么需要将此语法用于main()。然而,它会起作用。

#2


3  

The elipsis (...) are varargs. They are used when you want to create a method with any number of arguments. Oracle explains how varargs work in detail here.

elipsis(...)是varargs。当您想要创建具有任意数量参数的方法时,可以使用它们。 Oracle在此解释了varargs的详细工作原理。

#3


2  

This is called varargs which means any number of arguments can be passed of that type (String).

这称为varargs,这意味着可以传递该类型(String)的任意数量的参数。

1.it should be in final position

1.它应该处于最终位置

2.It will be processed as an array

2.它将作为一个数组处理

#4


1  

... is used for varargs.

...用于varargs。

For example

public void myMethod(Object... params) { }

public void myMethod(Object ... params){}

The params variable is optional and is treated as a nullable array of Objects.

params变量是可选的,被视为可以为对象的可空数组。

#1


7  

That's a notation from Java 5 for variable length argument lists. It is roughly equivalent to a String array, but lets you pass individual parameters that are combined into an array automatically.

这是Java 5中用于可变长度参数列表的符号。它大致相当于String数组,但允许您自动传递组合成数组的各个参数。

void mymethod(String... a) {
    for (String s : a) {
        ...
    }
}

mymethod("quick", "brown", "fox");

This makes sense only when you plan to call a method from your program, so I do not see why it would be desirable to use this syntax for your main(). It will work, however.

这只有在您计划从程序中调用方法时才有意义,因此我不明白为什么需要将此语法用于main()。然而,它会起作用。

#2


3  

The elipsis (...) are varargs. They are used when you want to create a method with any number of arguments. Oracle explains how varargs work in detail here.

elipsis(...)是varargs。当您想要创建具有任意数量参数的方法时,可以使用它们。 Oracle在此解释了varargs的详细工作原理。

#3


2  

This is called varargs which means any number of arguments can be passed of that type (String).

这称为varargs,这意味着可以传递该类型(String)的任意数量的参数。

1.it should be in final position

1.它应该处于最终位置

2.It will be processed as an array

2.它将作为一个数组处理

#4


1  

... is used for varargs.

...用于varargs。

For example

public void myMethod(Object... params) { }

public void myMethod(Object ... params){}

The params variable is optional and is treated as a nullable array of Objects.

params变量是可选的,被视为可以为对象的可空数组。