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

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);
  }
}

posted @ 2015-11-16 11:12  wewalk  阅读(356)  评论(0编辑  收藏  举报