It is 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
答案:varargs
6 个解决方案
#1
210
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.
#2
11
Yes, it's possible:
是的,这是可能的:
public void myMethod(int...numbers) { ... }
#3
10
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认证助理Java SE 7程序员学习指南2012
#4
4
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
#5
2
If you may use different types of arguments, then use :
如果您可以使用不同类型的参数,请使用:
public void foo(Object... x) {
String first = x.length > 0 ? (String)x[0] : "Hello";
int duration = x.length > 1 ? Integer.parseInt((String) x[1]) : 888;
}
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 .
这种方法的主要缺点是,如果可选参数属于不同类型,则会丢失静态类型检查。请看更多变化。
#6
0
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;
}
}
#1
210
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.
#2
11
Yes, it's possible:
是的,这是可能的:
public void myMethod(int...numbers) { ... }
#3
10
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认证助理Java SE 7程序员学习指南2012
#4
4
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
#5
2
If you may use different types of arguments, then use :
如果您可以使用不同类型的参数,请使用:
public void foo(Object... x) {
String first = x.length > 0 ? (String)x[0] : "Hello";
int duration = x.length > 1 ? Integer.parseInt((String) x[1]) : 888;
}
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 .
这种方法的主要缺点是,如果可选参数属于不同类型,则会丢失静态类型检查。请看更多变化。
#6
0
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;
}
}