Returns the minimum of an array. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Returns the minimum of an array.

Demo Code


//package com.java2s;

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

    /**
     * Returns the minimum of an array.
     */
    public static double min(double[] v) {
        double min = v[0];
        for (int i = 1; i < v.length; i++) {
            if (min > v[i]) {
                min = v[i];
            }
        }
        return (min);
    }

    /**
     * Returns the minimum of an array.
     */
    public static double min(double[][] v) {
        double min = min(v[0]);
        for (int i = 1; i < v.length; i++) {
            if (min > min(v[i])) {
                min = min(v[i]);
            }
        }
        return (min);
    }

    /**
     * Returns the minimum of an array.
     */
    public static int min(int[] v) {
        int min = v[0];
        for (int i = 1; i < v.length; i++) {
            if (min > v[i]) {
                min = v[i];
            }
        }
        return (min);
    }

    /**
     * Returns the minimum of an array.
     */
    public static int min(int[][] v) {
        int min = min(v[0]);
        for (int i = 1; i < v.length; i++) {
            if (min > min(v[i])) {
                min = min(v[i]);
            }
        }
        return (min);
    }
}

Related Tutorials