for each 循环+可变参数的小例子(JDK5)

来源:百度文库 编辑:神马文学网 时间:2024/07/06 00:20:33
今天整理以前的一些零碎代码,把一个for each 循环+可变参数的小例子(JDK5)和大家分享一下
1
2public class VariableArgs {
3
4    public static void main(String[] args) {
5        VariableArgs test = new VariableArgs();
6        test.show("1","2");
7        test.show("1","hermit","3");
8        test.showObject(123,"hermit");
9        test.addT(1,2,3,4);
10    }
11
12    public void show(Stringstrings) {
13        System.out.println(strings.length);
14        for(String str : strings) {
15            System.out.print(str+"\t");
16        }
17        System.out.println();
18    }
19
20    public void showObject(Objectobjects) {
21        System.out.println(objects.length);
22        for(Object obj : objects) {
23            System.out.print(obj.toString()+"\t");
24        }
25        System.out.println();
26    }
27
28    public void addT(intobj) {
29        System.out.println(obj.length);
30        int rs = 0;
31        for(int x : obj) {
32            rs+=x;
33        }
34        System.out.println(rs);
35    }
36
37}
38