Java Array Min Value min(byte[] array)

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

Description

Returns the minimum 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 min(byte[] array) 

Method Source Code

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

public class Main {
    /**/*w ww  .  j ava2  s  .c  o  m*/
     * <p>Gets the minimum of three <code>byte</code> values.</p>
     *
     * @param a  symbol 1
     * @param b  symbol 2
     * @param c  symbol 3
     * @return  the smallest of the values
     */
    public static byte min(byte a, byte b, byte c) {
        if (b < a)
            a = b;
        if (c < a)
            a = c;
        return a;
    }

    /**
     * <p>Returns the minimum 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 min(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 min
        byte min = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] < min)
                min = array[i];

        return min;
    }

    /**
     * <p>Gets the minimum 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 smallest of the values
     */
    public static double min(double a, double b, double c) {
        return Math.min(Math.min(a, b), c);
    }

    /**
     * <p>Returns the minimum 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 min(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 min
        double min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (Double.isNaN(array[i]))
                return Double.NaN;
            if (array[i] < min)
                min = array[i];
        }

        return min;
    }

    /**
     * <p>Gets the minimum 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 smallest of the values
     */
    public static float min(float a, float b, float c) {
        return Math.min(Math.min(a, b), c);
    }

    /**
     * <p>Returns the minimum 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 min(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 min
        float min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (Float.isNaN(array[i]))
                return Float.NaN;
            if (array[i] < min)
                min = array[i];
        }

        return min;
    }

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

    /**
     * <p>Returns the minimum 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 min(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 min
        int min = array[0];
        for (int j = 1; j < array.length; j++)
            if (array[j] < min)
                min = array[j];

        return min;
    }

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

    /**
     * <p>Returns the minimum 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 min(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 min
        long min = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] < min)
                min = array[i];

        return min;
    }

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

    /**
     * <p>Returns the minimum 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 min(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 min
        short min = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] < min)
                min = array[i];

        return min;
    }
}

Related

  1. maxLength(T[] array)
  2. maxMinValues(int[] array)
  3. maxMinValues(long[] array)
  4. maxValue(long[] array)
  5. min(byte[] arr)
  6. min(byte[] values)
  7. min(double values[], boolean[] mask)
  8. min(double values[], int size)
  9. min(double... a)