java省略号,在Java中使用省略号(...)?

I was looking through some code and saw the following notation. I'm somewhat unsure what the three dots mean and what you call them.

void doAction(Object...o);

Thanks.

解决方案

It means that this method can receive more than one Object as a parameter. To better understating check the following example from here:

The ellipsis (...) identifies a variable number of arguments, and is

demonstrated in the following summation method.

static int sum (int ... numbers)

{

int total = 0;

for (int i = 0; i < numbers.length; i++)

total += numbers [i];

return total;

}

Call the summation method with as many comma-delimited integer

arguments as you desire -- within the JVM's limits. Some examples: sum

(10, 20) and sum (18, 20, 305, 4).

This is very useful since it permits your method to became more abstract. Check also this nice example from SO, were the user takes advantage of the ... notation to make a method to concatenate string arrays in Java.

public static void test(int some, String... args) {

System.out.print("\n" + some);

for(String arg: args) {

System.out.print(", " + arg);

}

}

As mention in the comment section:

Also note that if the function passes other parameters of different

types than varargs parameter, the vararg parameter should be the last

parameter in the function declaration public void test (Typev ... v ,

Type1 a, Type2 b) or public void test(Type1 a, Typev ... v

recipientJids, Type2 b) - is illegal. ONLY public void test(Type1 a,

Type2 b, Typev ... v)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值