【JAVA学习路-think in java】p102:可变参数列表

package pkg;

class a{}


public class p102 {
	static void printFun1(Object[] args) {// Object
		for (Object tmp:args)
			System.out.print(tmp+",");
		System.out.println();
		
	}
	
	static void printFun2(int k,Object...args) {//Object 2
		System.out.print("K="+k+"	");
		for (Object obj:args) {
			System.out.print(obj+" ");
		}
		System.out.println();
	}
	
	static void printFun3(int j,String...args) {//String int double ... all is OK
		System.out.print("J="+j+"	");
		for (String str:args) {
			System.out.print(str+"/ ");
		}
		
	}
	
	
	public static void main(String[] args) {
		//func 1
		printFun1(new Object[]{new Integer(15),new Float(2.71828),new Double(3.14),"Unbelivable"});
		printFun1(new Object[]{"Here","you","see","six","strings","now!",new Integer(9527)});
		printFun1(new Object[]{new a(),new a()});
		System.out.println("------------    CUT LINE   ------------\n");
		
		//func 2
		printFun2(886,new Object[] {1,1,2,"object is so",5,8,13,3.14,"flexible"});
		printFun2(1,new Object[] {2.1,2.3,4,5,"abc"});
		System.out.println("------------    CUT LINE   ------------\n");
		
		//func 3
		printFun3(100,new String[] {"Tom","cat","&","jenny","mouse"});
		//printFun3(100,new String[] {"Tom","cat",100,"jenny","mouse"});//ERROR!	
	}
}


 

OUTPUT:

15,2.71828,3.14,Unbelivable,
Here,you,see,six,strings,now!,9527,
pkg.a@15db9742,pkg.a@6d06d69c,
------------    CUT LINE   ------------

K=886    1 1 2 object is so 5 8 13 3.14 flexible 
K=1    2.1 2.3 4 5 abc 
------------    CUT LINE   ------------

J=100    Tom/ cat/ &/ jenny/ mouse/ 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值