Returns the maximum of an array. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Returns the maximum 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(max(v));
    }//from   w w  w . j a  va2 s .  c o m

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

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

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

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

Related Tutorials