For example, Java's own String.format()
supports a variable number of arguments.
例如,Java自己的String.format()支持可变数量的参数。
String.format("Hello %s! ABC %d!", "World", 123);
//=> Hello World! ABC 123!
How can I make my own function that accepts a variable number of arguments?
我怎样才能使我自己的函数接受可变数量的参数呢?
Follow-up question:
后续问题:
I'm really trying to make a convenience shortcut for this:
我想为它提供一个方便快捷的捷径:
System.out.println( String.format("...", a, b, c) );
So that I can call it as something less verbose like this:
所以我可以这样称呼它
print("...", a, b, c);
How can I achieve this?
我如何做到这一点?
7 个解决方案
#1
105
You could write a convenience method:
你可以写一个方便的方法:
public PrintStream print(String format, Object... arguments) {
return System.out.format(format, arguments);
}
But as you can see, you've simply just renamed format
(or printf
).
但是正如您所看到的,您只是简单地重命名了format(或printf)。
Here's how you could use it:
你可以这样使用:
private void printScores(Player... players) {
for (int i = 0; i < players.length; ++i) {
Player player = players[i];
String name = player.getName();
int score = player.getScore();
// Print name and score followed by a newline
System.out.format("%s: %d%n", name, score);
}
}
// Print a single player, 3 players, and all players
printScores(player1);
System.out.println();
printScores(player2, player3, player4);
System.out.println();
printScores(playersArray);
// Output
Abe: 11
Bob: 22
Cal: 33
Dan: 44
Abe: 11
Bob: 22
Cal: 33
Dan: 44
Note there's also the similar System.out.printf
method that behaves the same way, but if you peek at the implementation, printf
just calls format
, so you might as well use format
directly.
注意这里也有类似的系统。printf方法的行为方式相同,但是如果您查看实现,printf只调用format,所以您最好直接使用format。
- Varargs
- 可变参数
PrintStream#format(String format, Object... args)
- PrintStream #格式(字符串格式,对象……args)
PrintStream#printf(String format, Object... args)
- PrintStream # printf(字符串格式,对象…args)
#2
25
This is known as varargs see the link here for more details
这被称为varargs,详情请参阅这里的链接
In past java releases, a method that took an arbitrary number of values required you to create an array and put the values into the array prior to invoking the method. For example, here is how one used the MessageFormat class to format a message:
在过去的java版本中,一种使用任意数量的值的方法需要创建一个数组,并在调用该方法之前将值放入数组中。例如,下面是如何使用MessageFormat类来格式化消息的:
Object[] arguments = {
new Integer(7),
new Date(),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet "
+ "{0,number,integer}.", arguments);
It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process. Furthermore, it is upward compatible with preexisting APIs. So, for example, the MessageFormat.format method now has this declaration:
仍然需要在数组中传递多个参数,但是varargs特性自动并隐藏了进程。此外,它向上兼容现有的api。例如,MessageFormat。格式方法现在有以下声明:
public static String format(String pattern,
Object... arguments);
#3
8
Take a look at the Java guide on varargs.
看看关于varargs的Java指南。
You can create a method as shown below. Simply call System.out.printf
instead of System.out.println(String.format(...
.
您可以创建一个方法,如下所示。简单地调用system . out。printf代替System.out.println(String.format(....
public static void print(String format, Object... args) {
System.out.printf(format, args);
}
Alternatively, you can just use a static import if you want to type as little as possible. Then you don't have to create your own method:
或者,如果您想尽可能少输入,您可以使用静态导入。那么你就不需要创建自己的方法:
import static java.lang.System.out;
out.printf("Numer of apples: %d", 10);
#4
3
This is just an extension to above provided answers.
这只是对上述答案的扩展。
- There can be only one variable argument in the method.
- 方法中只能有一个变量参数。
- Variable argument (varargs) must be the last argument.
- 变量参数(varargs)必须是最后一个参数。
Clearly explained here and rules to follow to use Variable Argument.
这里清楚地解释了使用变量参数的规则。
#5
2
The following will create a variable length set of arguments of the type of string:
下面将创建字符串类型的可变长度参数集:
print(String arg1, String... arg2)
You can then refer to arg2
as an array of Strings. This is a new feature in Java 5.
然后可以将arg2作为字符串数组引用。这是Java 5中的一个新特性。
#6
0
The variable arguments must be the last of the parameters specified in your function declaration. If you try to specify another parameter after the variable arguments, the compiler will complain since there is no way to determine how many of the parameters actually belong to the variable argument.
变量参数必须是函数声明中指定的最后一个参数。如果您试图在变量参数之后指定另一个参数,编译器将会抱怨,因为无法确定有多少参数实际上属于变量参数。
void print(final String format, final String... arguments) {
System.out.format( format, arguments );
}
#7
-2
You can pass all similar type values in the function while calling it. In the function definition put a array so that all the passed values can be collected in that array. e.g. .
您可以在调用函数时传递所有类似的类型值。在函数定义中,放置一个数组,以便在该数组中收集所有传递的值。例如。
static void demo (String ... stringArray) {
your code goes here where read the array stringArray
}
#1
105
You could write a convenience method:
你可以写一个方便的方法:
public PrintStream print(String format, Object... arguments) {
return System.out.format(format, arguments);
}
But as you can see, you've simply just renamed format
(or printf
).
但是正如您所看到的,您只是简单地重命名了format(或printf)。
Here's how you could use it:
你可以这样使用:
private void printScores(Player... players) {
for (int i = 0; i < players.length; ++i) {
Player player = players[i];
String name = player.getName();
int score = player.getScore();
// Print name and score followed by a newline
System.out.format("%s: %d%n", name, score);
}
}
// Print a single player, 3 players, and all players
printScores(player1);
System.out.println();
printScores(player2, player3, player4);
System.out.println();
printScores(playersArray);
// Output
Abe: 11
Bob: 22
Cal: 33
Dan: 44
Abe: 11
Bob: 22
Cal: 33
Dan: 44
Note there's also the similar System.out.printf
method that behaves the same way, but if you peek at the implementation, printf
just calls format
, so you might as well use format
directly.
注意这里也有类似的系统。printf方法的行为方式相同,但是如果您查看实现,printf只调用format,所以您最好直接使用format。
- Varargs
- 可变参数
PrintStream#format(String format, Object... args)
- PrintStream #格式(字符串格式,对象……args)
PrintStream#printf(String format, Object... args)
- PrintStream # printf(字符串格式,对象…args)
#2
25
This is known as varargs see the link here for more details
这被称为varargs,详情请参阅这里的链接
In past java releases, a method that took an arbitrary number of values required you to create an array and put the values into the array prior to invoking the method. For example, here is how one used the MessageFormat class to format a message:
在过去的java版本中,一种使用任意数量的值的方法需要创建一个数组,并在调用该方法之前将值放入数组中。例如,下面是如何使用MessageFormat类来格式化消息的:
Object[] arguments = {
new Integer(7),
new Date(),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet "
+ "{0,number,integer}.", arguments);
It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process. Furthermore, it is upward compatible with preexisting APIs. So, for example, the MessageFormat.format method now has this declaration:
仍然需要在数组中传递多个参数,但是varargs特性自动并隐藏了进程。此外,它向上兼容现有的api。例如,MessageFormat。格式方法现在有以下声明:
public static String format(String pattern,
Object... arguments);
#3
8
Take a look at the Java guide on varargs.
看看关于varargs的Java指南。
You can create a method as shown below. Simply call System.out.printf
instead of System.out.println(String.format(...
.
您可以创建一个方法,如下所示。简单地调用system . out。printf代替System.out.println(String.format(....
public static void print(String format, Object... args) {
System.out.printf(format, args);
}
Alternatively, you can just use a static import if you want to type as little as possible. Then you don't have to create your own method:
或者,如果您想尽可能少输入,您可以使用静态导入。那么你就不需要创建自己的方法:
import static java.lang.System.out;
out.printf("Numer of apples: %d", 10);
#4
3
This is just an extension to above provided answers.
这只是对上述答案的扩展。
- There can be only one variable argument in the method.
- 方法中只能有一个变量参数。
- Variable argument (varargs) must be the last argument.
- 变量参数(varargs)必须是最后一个参数。
Clearly explained here and rules to follow to use Variable Argument.
这里清楚地解释了使用变量参数的规则。
#5
2
The following will create a variable length set of arguments of the type of string:
下面将创建字符串类型的可变长度参数集:
print(String arg1, String... arg2)
You can then refer to arg2
as an array of Strings. This is a new feature in Java 5.
然后可以将arg2作为字符串数组引用。这是Java 5中的一个新特性。
#6
0
The variable arguments must be the last of the parameters specified in your function declaration. If you try to specify another parameter after the variable arguments, the compiler will complain since there is no way to determine how many of the parameters actually belong to the variable argument.
变量参数必须是函数声明中指定的最后一个参数。如果您试图在变量参数之后指定另一个参数,编译器将会抱怨,因为无法确定有多少参数实际上属于变量参数。
void print(final String format, final String... arguments) {
System.out.format( format, arguments );
}
#7
-2
You can pass all similar type values in the function while calling it. In the function definition put a array so that all the passed values can be collected in that array. e.g. .
您可以在调用函数时传递所有类似的类型值。在函数定义中,放置一个数组,以便在该数组中收集所有传递的值。例如。
static void demo (String ... stringArray) {
your code goes here where read the array stringArray
}