zhaoshijie 发表于 2013-1-26 13:38:18

JAVA可变参数之经典用法

关键字:JAVA可变参数之经典用法

说明:可变参数 只能放在函数的最后位置,可变参数最终被编辑为 数组(可以去被编译好的class文件中看看 就明白了)


/**
   * @param intArray
   */
    public static void dealArrayForInt(int... intArray){
    for(int each : intArray){
    System.out.println(each);
    }
    }
   
    public static void test1(){
    int[] intArray = {1, 2, 3};   
    dealArrayForInt(intArray);//通过编译,正常运行   

    dealArrayForInt();
    dealArrayForInt(1);
    dealArrayForInt(1,2);
    dealArrayForInt(1,2,3);
   
    // ...
    }
   
    public static void dealArrayForList(List<String>... intArray){
    for(List<String> each : intArray){
    for(String ss : each){
    System.out.println(ss);
    }
    }
    }
   
    public static void test2(){
    dealArrayForList();
    dealArrayForList(new ArrayList(),new ArrayList(),new ArrayList());
    dealArrayForList(new ArrayList(),new ArrayList(),new ArrayList());
    // ...
    }
   
      
    public static void main(String args[]){   
    dealArrayForList(new ArrayList(),new ArrayList(),new ArrayList());
    }
   
    /**
   * 说明:可变参数类型必须作为参数列表的最后一项(而且 一个函数只能有一个可变参数)
   * @param intArray
   */
    public static void dealArrayInt(int a,int... intArray){
    for(int each : intArray){
    System.out.println(each);
    }
    }
   
    // ====================================优先级==================
   
    public static void zhao1(int aa,int bb){
    System.out.println("不可变参数 优先调用");
    }
   
    public static void zhao1(int...c){
    System.out.println("可变参数 的优先级是最低的");
    }
   
    public void test3(){
    zhao1(6,8);
    }
   
   
   
}
页: [1]
查看完整版本: JAVA可变参数之经典用法