首页 > 其他 > 详细

静态内部类:一次遍历求最大最小值(代码示例)

时间:2015-11-16 12:36:28      阅读:201      评论:0      收藏:0      [点我收藏+]

public class StaticInnerClassTest{

  public static void main(String[] args){

    double[] d = new double[20];

    for(int i = 0;i < d.length;i++)
      d[i] = 100*Math.random();

    ArrayAlg.Pair p = ArrayAlg.minmax(d);

    System.out.println("min = " + p.getFirst());
    System.out.println("max = " + p.getSecond());
  }

}

class ArrayAlg{

  public static class Pair{

    private double first;
    private double second;

    public Pair(double f,double s){
      first = f;
      second = s;
    }

    public double getFirst(){
      return first;
    }

    public double getSecond(){
      return second;
    }
  }

  public static Pair minmax(double[] values){

    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;

    for(double v : values){
      if(min > v) min =v;
      if(max < v) max =v;
    }

    return new Pair(min,max);
  }
}

静态内部类:一次遍历求最大最小值(代码示例)

原文:http://www.cnblogs.com/yuhebin/p/4968380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!