minimum value in two dimensional array - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

minimum value in two dimensional array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] arr = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
                37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(minimum(arr));
    }//from   w  w w . j  a  v  a  2s  .  c  o m

    public static double minimum(double[] arr) {
        double minVal = Double.POSITIVE_INFINITY;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < minVal) {
                minVal = arr[i];
            }
        }
        return minVal;
    }

    public static double minimum(double[][] arr) {
        double minVal = Double.POSITIVE_INFINITY;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                minVal = Math.min(minVal, minimum(arr[i]));
            }
        }
        return minVal;
    }

    public static double minimum(double[][][] arr) {
        double minVal = Double.POSITIVE_INFINITY;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                minVal = Math.min(minVal, minimum(arr[i]));
            }
        }
        return minVal;
    }
}

Related Tutorials