java 获取数组的最大值和最小值

来源:互联网 发布:网络触摸广告机 编辑:程序博客网 时间:2024/04/28 02:17
package com;public class StaticInnerClassTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubdouble[] d = new double[5];d[0] = 1;d[1] = 1.9;d[2] = 2.0;d[3] = 3.8;d[4] = 5.7;ArryAlg.Pair p= ArryAlg.minmax(d);System.out.println("最小值"+p.getFirst());System.out.println("最大值:"+p.getSecond());}}class ArryAlg {public static class Pair {public Pair(double f, double s) {first = f;second = s;}public double getFirst() {return first;}// public void setFirst(double first) {// this.first = first;// }public double getSecond() {return second;}// public void setSecond(double second) {// this.second = second;// }private double first;private double 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);}}
0 0
原创粉丝点击