java 方法任意长度参数_函数-如何创建接受可变数量的参数的Java方法?

函数-如何创建接受可变数量的参数的Java方法?

例如,Java自己的String.format()支持可变数量的参数。

String.format("Hello %s! ABC %d!", "World", 123);

//=> Hello World! ABC 123!

我怎样才能使自己的函数接受可变数量的参数?

后续问题:

我真的想为此方便快捷:

System.out.println( String.format("...", a, b, c) );

这样我就可以这样称呼它:

print("...", a, b, c);

我怎样才能做到这一点?

7个解决方案

112 votes

您可以编写一个便捷方法:

public PrintStream print(String format, Object... arguments) {

return System.out.format(format, arguments);

}

但是正如您所看到的,您只是将其重命名为PrintStream#printf(String format, Object... args)(或printf)。

使用方法如下:

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

请注意,还有类似的PrintStream#printf(String format, Object... args)方法,其行为方式相同,但是如果您查看实现,则printf只会调用format,因此您最好直接使用format。

可变参数

PrintStream#printf(String format, Object... args)

PrintStream#printf(String format, Object... args)

Nate W. answered 2019-09-30T09:44:34Z

25 votes

这就是所谓的varargs,有关更多详细信息,请参见此处的链接。

在过去的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);

仍然必须在数组中传递多个参数,但是varargs功能可以自动执行并隐藏进程。 此外,它与先前存在的API向上兼容。 因此,例如,MessageFormat.format方法现在具有以下声明:

public static String format(String pattern,

Object... arguments);

Paul Whelan answered 2019-09-30T09:45:13Z

9 votes

看一下有关varargs的Java指南。

您可以创建如下所示的方法。 只需致电System.out.printf,而不是System.out.println(String.format(...。

public static void print(String format, Object... args) {

System.out.printf(format, args);

}

另外,如果您想尽可能少地键入内容,则可以只使用静态导入。 然后,您不必创建自己的方法:

import static java.lang.System.out;

out.printf("Numer of apples: %d", 10);

dogbane answered 2019-09-30T09:45:50Z

3 votes

这只是上述提供的答案的扩展。

该方法中只能有一个变量参数。

可变参数(varargs)必须是最后一个参数。

此处明确说明了使用变量参数的规则和遵循的规则。

Prashanth Debbadwar answered 2019-09-30T09:46:35Z

2 votes

下面将创建一个可变长度的字符串类型的参数集:

print(String arg1, String... arg2)

然后,您可以将arg2称为字符串数组。 这是Java 5中的新功能。

Bradley Stacey answered 2019-09-30T09:47:06Z

0 votes

变量参数必须是函数声明中指定的最后一个参数。 如果尝试在变量参数之后指定另一个参数,则编译器将抱怨,因为无法确定实际上有多少个参数属于变量参数。

void print(final String format, final String... arguments) {

System.out.format( format, arguments );

}

Izza answered 2019-09-30T09:47:32Z

-2 votes

您可以在调用函数时在函数中传递所有类似的类型值。在函数定义中放置一个数组,以便所有传递的值都可以收集在该数组中。例如。

static void demo (String ... stringArray) {

your code goes here where read the array stringArray

}

Sumer answered 2019-09-30T09:47:57Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值