码迷,mamicode.com
首页 > 其他好文 > 详细

5-5 可变参数的方法

时间:2018-01-07 17:35:13      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:可变参   format   bsp   turn   ati   3.1   pos   stat   new   

import java.io.PrintStream;


public class ParamVaO {

    /**
     * @author:lixh
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        

        
        double[] a =  new double[]{3.1,4.5,1.1};
        getMax(3.1,4.5,1.1);
        getMax(a);
        
    }

    /**
     * 参数变量可变的方法
     * 
     * System.out.printf(format, args);
     * 源码
     *  public PrintStream printf(String format, Object ... args) {
     *      return format(format, args);
     *  }
     *  对于方法printf来说 Object ... args 与 Object[] args 一样
     * 编译器对每次的调用进行转换,以便将参数绑定到数组上,并在必要时候进行自动装箱
     * 
     * 
     * 
     *  getMax(3.1,4.5,1.1);
     *     编译器将new double[]{3.1,4.5,1.1};传递给方法getMax
     * 
     * 参数可变变量必须放到最后一位   public static double getMax(double ... arg,String string){}报错
     * 
     * 可以将最后一个参数是数组方法重新定义成可变参数的方法,而且不会破坏已经存在的代码
     */
    public static double getMax(double ... arg){
        double Max = 0.0d;
        for (double d : arg) {
            if (Max<d) {
                Max = d;
            }
        }
        return Max;
    } 
    
    /*public static double getMax(double[] arg){
        double Max = 0.0d;
        for (double d : arg) {
            if (Max<d) {
                Max = d;
            }
        }
        return Max;
    } */
    
    
}

 

5-5 可变参数的方法

标签:可变参   format   bsp   turn   ati   3.1   pos   stat   new   

原文地址:https://www.cnblogs.com/lxh520/p/8228178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!