六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

JAVA可变参数之经典用法

[复制链接]

升级  9.33%

66

主题

66

主题

66

主题

举人

Rank: 3Rank: 3

积分
228
 楼主| 发表于 2013-1-26 13:38:18 | 显示全部楼层 |阅读模式
关键字: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);
    }
   
   
   
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表