for each 循环+可变参数的小例子(JDK5)
来源:百度文库 编辑:神马文学网 时间:2024/07/09 03:21:39
今天整理以前的一些零碎代码,把一个for each 循环+可变参数的小例子(JDK5)和大家分享一下
1
2 public 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(String strings) {
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(Object objects) {
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(int obj) {
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38