引元数量可变的方法

Posted on 2007-09-19 13:06 流浪韩 阅读(279) 评论(0)  编辑  收藏 所属分类: J2SE

引元数量可变的方法

public class Test {
    public void test(String... body){

//String...代表String的数组,长度由传进来时的数组长度决定
        for (int i = 0; i < body.length; i++) {
            System.out.print(body[i]);
        }

        System.out.println();
    }
}

public class Main {
    public static void main(String[] args) {
        Test t=new Test();
        t.test("hello,","hi");
        t.test("good morning");
        t.test("good afternoon,","good evening,","good night");
    }
}
输出结果:
hello,hi
good morning
good afternoon,good evening,good night


只有注册用户登录后才能发表评论。


网站导航: