Posted on 2005-09-12 23:17
Justfly Shi 阅读(823)
评论(0) 编辑 收藏 所属分类:
Study Tiger
如下列代码所示
1、传入的可变参数将做为一个数组处理
2、一个函数中只能有一个可变参数列,并且只能在函数参数定义的最后。
package cn.justfly.study.tiger;
/**
* Sample of using varargs
* @author Justfly Shi
* created at 2005-8-31 0:38:19
*/
public class Varargs {
public void printObjectArgs(Object objects ){
for(Object o:objects)// objects is an Object Array
{
System.out.println(o);
}
System.out.println("var count:"+ objects.length);
}
public void printFloats(float fs ){
for(float f:fs){
System.out.println(f);
}
System.out.println("var count:"+ fs.length);
}
// only one param can be var args,and should be placed at the end of param list
// public void printArgs(Integer integers,String objects ){}
/**
* @param args
*/
public static void main(String[] args) {
Varargs vars=new Varargs();
vars.printObjectArgs(new Integer(1),new Integer(2),3,"abc");
vars.printFloats(1.2f,1.3f);
}
}
输出结果:
1
2
3
abc
var count:4
1.2
1.3
var count:2