方法的 Java 变量号或参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2330942/
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
Java variable number or arguments for a method
提问by user69514
Is it possible to declare a method that will allow a variable number of parameters ?
是否可以声明一个允许可变数量参数的方法?
What is the symbolism used in the definition that indicate that the method should allow a variable number of parameters?
定义中使用的符号是什么,表明该方法应该允许可变数量的参数?
Answer:varargs
答案:可变参数
回答by BalusC
That's correct. You can find more about it in the Oracle guide on varargs.
这是正确的。您可以在有关 varargs 的 Oracle 指南中找到有关它的更多信息。
Here's an example:
下面是一个例子:
void foo(String... args) {
for (String arg : args) {
System.out.println(arg);
}
}
which can be called as
这可以称为
foo("foo"); // Single arg.
foo("foo", "bar"); // Multiple args.
foo("foo", "bar", "lol"); // Don't matter how many!
foo(new String[] { "foo", "bar" }); // Arrays are also accepted.
foo(); // And even no args.
回答by Roland Bouman
Yup...since Java 5: http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html
是的...从 Java 5 开始:http: //java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html
回答by Dolph
Yes, it's possible:
是的,这是可能的:
public void myMethod(int... numbers) { /* your code */ }
回答by Abey Ella
Variable number of arguments
It is possible to pass a variable number of arguments to a method. However, there are some restrictions:
可以将可变数量的参数传递给方法。但是,有一些限制:
- The variable number of parameters must all be the same type
- They are treated as an array within the method
- They must be the last parameter of the method
- 可变数量的参数必须都是相同的类型
- 它们在方法中被视为数组
- 它们必须是方法的最后一个参数
To understand these restrictions, consider the method, in the following code snippet, used to return the largest integer in a list of integers:
要了解这些限制,请考虑以下代码片段中用于返回整数列表中最大整数的方法:
private static int largest(int... numbers) {
int currentLargest = numbers[0];
for (int number : numbers) {
if (number > currentLargest) {
currentLargest = number;
}
}
return currentLargest;
}
source Oracle Certified Associate Java SE 7 Programmer Study Guide 2012
来源 Oracle Certified Associate Java SE 7 Programmer Study Guide 2012
回答by T.Todua
For different types of arguments, there is 3-dots:
对于不同类型的参数,有3 个点:
public void foo(Object... x) {
String myVar1 = x.length > 0 ? (String)x[0] : "Hello";
int myVar2 = x.length > 1 ? Integer.parseInt((String) x[1]) : 888;
}
Then call it
然后调用它
foo("Hii");
foo("Hii", 146);
for security, use like this:
if (!(x[0] instanceof String)) { throw new IllegalArgumentException("..."); }
为了安全起见,请像这样使用:
if (!(x[0] instanceof String)) { throw new IllegalArgumentException("..."); }
The main drawback of this approach is that if optional parameters are of different types you lose static type checking. Please, see more variations.
这种方法的主要缺点是,如果可选参数的类型不同,则会丢失静态类型检查。请查看更多变化。
回答by Code_Eat_Sleep
Yes Java allows vargs
in method parameter .
是 Java 允许vargs
方法参数。
public class??Varargs
{
public?int?add(int...?numbers)
{?
int?result =?1;?
for(int?number: numbers)
{
result= result+number;??
}??return?result;?
}
}