Java中的三个点"..."

转自:https://blog.csdn.net/it_faquir/article/details/49131173
 java1.5引入。"…"必须是方法的最后一个形参,表示多个(0,1,2,…)参数,类似数组参数,使用数组传实参
但是与数组参数又有区别,"…"表示可变长参数(多个参数),数组参数只是一个参数。详见下面举例:

public class Test {
	void t1(String... a) {
		System.out.println("t1");
		for (String s : a)
			System.out.printf(" " + s);
		System.out.println();
	}
 
	void t2(String[] a) {
		System.out.println("t2");
		for (String s : a)
			System.out.printf(" " + s);
		System.out.println();
	}
 
	public static void main(String[] args) {
		String a[] = { "a", "b", "d", "e", "f", "g" };
		Test t = new Test();
		t.t1(a);
		t.t2(a);
		// 区别
		t.t1();// 可不传
		// t.t2();//必须传参数,否则报错
		t.t1("1", "2", "3", "4");// 也可以一个一个的传,t2则不可以
	}
}

运行结果:

t1
 a b d e f g
t2
 a b d e f g
t1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值