黑马程序员--方法的可变参数简单介绍

--------- android培训java培训期待与您交流 ---------

方法的可变参数

1,介绍

方法的可变参数是JDK1.5版本出现的新特性。

在使用时需注意当方法含有多个参数时,可变参数一定要放到参数列表的后面

public static void method(String...str)

{}

public static void method(int a,char b,String...str)

{}

这时可变参数一定要放在参数列表的最后面,如上所示:

可变参数的表示方式就是: 数据类型...变量名

2,可变参数
  其实就是一种数组参数的简写形式。
  不用每一次都手动的建立数组对象。
  只要将要操作的元素作为参数传递即可。
  隐式将这些参数封装成了数组。

举例如下所示

 

 1 class  ParamMethodDemo
 2 {
 3     public static void main(String[] args) 
 4     {
 5         //show(3,4);
 6         /*
 7         //虽然少定义了多个方法。
 8         但是每次都要定义一个数组。作为实际参数。
 9 
10         int[] arr = {3,4};
11         show(arr);
12 
13         int[] arr1 = {2,3,4,5};
14         show(arr1);
15         */
16 
17         
18         show("haha",2,3,4,5,6);
19         //show(2,3,4,5,6,4,2,35,9,"heh");
20         //show();
21 
22     }
23     public static void show(String str,int... arr)
24     {
25         System.out.println(arr.length);
26     }
27     
28     /*
29     //没有利用可变参数时需要定义多个方法
30     public static  void show(int a,int b)
31     {
32         System.out.println(a+","+b);
33     }
34     public static  void show(int a,int b,int c)
35     {}
36     */
37 }

 

 

 

 

 

 

 --------- android培训java培训期待与您交流 ----------

 

                             详细请查看:http://edu.csdn.net/heima/

 

 

 

posted on 2012-08-06 17:21  doublewinwin  阅读(189)  评论(0编辑  收藏  举报