Gets the maximum value in the array. - Java Collection Framework

Java examples for Collection Framework:Array Search

Description

Gets the maximum value in the array.

Demo Code


//package com.java2s;

public class Main {
    /**//from  w  ww .j  a va2 s  .co  m
     * Gets the maximum value in the array.
     * @param input
     * @return
     */
    public static short max(short[] input) {
        short max = Short.MIN_VALUE;
        for (int i = 0; i < input.length; i++) {
            if (input[i] > max) {
                max = input[i];
            }
        }
        return max;
    }

    /**
     * Gets the maximum value in the array.
     * @param input
     * @return
     */
    public static double max(double[] input) {
        double max = -Double.MAX_VALUE;
        for (int i = 0; i < input.length; i++) {
            if (input[i] > max) {
                max = input[i];
            }
        }
        return max;
    }
}

Related Tutorials