数组最值

来源:互联网 发布:闪电的利用知乎 编辑:程序博客网 时间:2024/05/20 14:25

可以返回两个值

/**比较一次获得数组中的最大值最小值*/package com.something;public class Min_Max {    public static void main(String[] args) {        // TODO Auto-generated method stub        double [] dd = {2,23,1,4,32,89,-2};        Comp cm = new Comp(dd);        Comp.Pair pp = cm.compair();        System.out.println("min"+pp.getfir()+",max"+pp.getsec());           /*double min=dd[0];        double max=dd[0];        for (int i=0;i<dd.length;i++){            if(min>dd[i]){                min=dd[i];            }            if(max<dd[i]){                max=dd[i];            }        }        System.out.println("max : "+max+"; min : "+min);*/          }}class Comp{    double[] d = new double[6];    Comp(double[] d){    this.d=d;    }    //返回的值是Pair类型的    public Pair compair(){        double min=d[0];        double max=d[0];        for(double v:d){            if(min>v){min=v;}            if(max<v){max=v;}                   }        return new Pair(min,max);      }        //把Pair类作为静态内部类       //创建一个可以返回两个值的类,通过调用 get 方法分别获得链各个返回值    public static class Pair {        private double first;        private double second;        Pair(double first,double second){            this.first=first;            this.second=second;        }               public double getfir(){            return first;        }        public double getsec(){            return second;        }    }    }
0 0
原创粉丝点击