Java Array Max Value max(byte[] array)

Here you can find the source of max(byte[] array)

Description

Returns the maximum symbol in an array.

License

Open Source License

Parameter

Parameter Description
array an array, must not be null or empty

Exception

Parameter Description
IllegalArgumentException if <code>array</code> is empty

Return

the minimum symbol in the array

Declaration

public static byte max(byte[] array) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w w  w .  j  a v  a2s.c o m*/
     * <p>Gets the maximum of three <code>byte</code> values.</p>
     *
     * @param a  symbol 1
     * @param b  symbol 2
     * @param c  symbol 3
     * @return  the largest of the values
     */
    public static byte max(byte a, byte b, byte c) {
        if (b > a)
            a = b;
        if (c > a)
            a = c;
        return a;
    }

    /**
     * <p>Returns the maximum symbol in an array.</p>
     *
     * @param array  an array, must not be null or empty
     * @return the minimum symbol in the array
     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>array</code> is empty
     */
    public static byte max(byte[] array) {
        // Validates input
        if (array == null)
            throw new IllegalArgumentException(
                    "The ArrayEx must not be null");
        else if (array.length == 0)
            throw new IllegalArgumentException("ArrayEx cannot be empty.");

        // Finds and returns max
        byte max = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] > max)
                max = array[i];

        return max;
    }

    /**
     * <p>Gets the maximum of three <code>double</code> values.</p>
     *
     * <p>If any symbol is <code>NaN</code>, <code>NaN</code> is
     * returned. Infinity is handled.</p>
     *
     * @param a  symbol 1
     * @param b  symbol 2
     * @param c  symbol 3
     * @return  the largest of the values
     */
    public static double max(double a, double b, double c) {
        return Math.max(Math.max(a, b), c);
    }

    /**
     * <p>Returns the maximum symbol in an array.</p>
     *
     * @param array  an array, must not be null or empty
     * @return the minimum symbol in the array
     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>array</code> is empty
     */
    public static double max(double[] array) {
        // Validates input
        if (array == null)
            throw new IllegalArgumentException(
                    "The ArrayEx must not be null");
        else if (array.length == 0)
            throw new IllegalArgumentException("ArrayEx cannot be empty.");

        // Finds and returns max
        double max = array[0];
        for (int j = 1; j < array.length; j++) {
            if (Double.isNaN(array[j]))
                return Double.NaN;
            if (array[j] > max)
                max = array[j];
        }

        return max;
    }

    /**
     * <p>Gets the maximum of three <code>float</code> values.</p>
     *
     * <p>If any symbol is <code>NaN</code>, <code>NaN</code> is
     * returned. Infinity is handled.</p>
     *
     * @param a  symbol 1
     * @param b  symbol 2
     * @param c  symbol 3
     * @return  the largest of the values
     */
    public static float max(float a, float b, float c) {
        return Math.max(Math.max(a, b), c);
    }

    /**
     * <p>Returns the maximum symbol in an array.</p>
     *
     * @param array  an array, must not be null or empty
     * @return the minimum symbol in the array
     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>array</code> is empty
     */
    public static float max(float[] array) {
        // Validates input
        if (array == null)
            throw new IllegalArgumentException(
                    "The ArrayEx must not be null");
        else if (array.length == 0)
            throw new IllegalArgumentException("ArrayEx cannot be empty.");

        // Finds and returns max
        float max = array[0];
        for (int j = 1; j < array.length; j++) {
            if (Float.isNaN(array[j]))
                return Float.NaN;
            if (array[j] > max)
                max = array[j];
        }

        return max;
    }

    /**
     * <p>Gets the maximum of three <code>int</code> values.</p>
     *
     * @param a  symbol 1
     * @param b  symbol 2
     * @param c  symbol 3
     * @return  the largest of the values
     */
    public static int max(int a, int b, int c) {
        if (b > a)
            a = b;
        if (c > a)
            a = c;
        return a;
    }

    /**
     * <p>Returns the maximum symbol in an array.</p>
     *
     * @param array  an array, must not be null or empty
     * @return the minimum symbol in the array
     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>array</code> is empty
     */
    public static int max(int[] array) {
        // Validates input
        if (array == null)
            throw new IllegalArgumentException(
                    "The ArrayEx must not be null");
        else if (array.length == 0)
            throw new IllegalArgumentException("ArrayEx cannot be empty.");

        // Finds and returns max
        int max = array[0];
        for (int j = 1; j < array.length; j++)
            if (array[j] > max)
                max = array[j];

        return max;
    }

    /**
     * <p>Gets the maximum of three <code>long</code> values.</p>
     *
     * @param a  symbol 1
     * @param b  symbol 2
     * @param c  symbol 3
     * @return  the largest of the values
     */
    public static long max(long a, long b, long c) {
        if (b > a)
            a = b;
        if (c > a)
            a = c;
        return a;
    }

    /**
     * <p>Returns the maximum symbol in an array.</p>
     *
     * @param array  an array, must not be null or empty
     * @return the minimum symbol in the array
     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>array</code> is empty
     */
    public static long max(long[] array) {
        // Validates input
        if (array == null)
            throw new IllegalArgumentException(
                    "The ArrayEx must not be null");
        else if (array.length == 0)
            throw new IllegalArgumentException("ArrayEx cannot be empty.");

        // Finds and returns max
        long max = array[0];
        for (int j = 1; j < array.length; j++)
            if (array[j] > max)
                max = array[j];

        return max;
    }

    /**
     * <p>Gets the maximum of three <code>short</code> values.</p>
     *
     * @param a  symbol 1
     * @param b  symbol 2
     * @param c  symbol 3
     * @return  the largest of the values
     */
    public static short max(short a, short b, short c) {
        if (b > a)
            a = b;
        if (c > a)
            a = c;
        return a;
    }

    /**
     * <p>Returns the maximum symbol in an array.</p>
     *
     * @param array  an array, must not be null or empty
     * @return the minimum symbol in the array
     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>array</code> is empty
     */
    public static short max(short[] array) {
        // Validates input
        if (array == null)
            throw new IllegalArgumentException(
                    "The ArrayEx must not be null");
        else if (array.length == 0)
            throw new IllegalArgumentException("ArrayEx cannot be empty.");

        // Finds and returns max
        short max = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] > max)
                max = array[i];

        return max;
    }
}

Related

  1. findMaxOverlap(final int[] overlapRange, final int[] outputRange, final int[] min, final int[] max)
  2. findMaxValue(int[] array)
  3. findMaxValueIndex(double[] d)
  4. max(boolean useInt, int... value)
  5. max(byte[] arr)
  6. max(byte[] array)
  7. max(byte[] lArray, byte[] rArray)
  8. max(byte[] values)
  9. max(D... comparables)