Java Array Min Value min(int[] list)

Here you can find the source of min(int[] list)

Description

Find the minimum value in an array.

License

Open Source License

Parameter

Parameter Description
list the source array

Return

The minimum value

Declaration

static public final int min(int[] list) 

Method Source Code

//package com.java2s;
/*//from ww  w.j  av a  2 s  .com
 * Copyright (c) 2013 maybites.ch
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal 
 *  in the Software without restriction, including without limitation the rights 
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 *  copies of the Software, and to permit persons to whom the Software is furnished
 *  to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class Main {
    static final String ERROR_MIN_MAX = "Cannot use min() or max() on an empty array.";

    static public final int min(int a, int b) {
        return (a < b) ? a : b;
    }

    static public final float min(float a, float b) {
        return (a < b) ? a : b;
    }

    static public final int min(int a, int b, int c) {
        return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
    }

    static public final float min(float a, float b, float c) {
        return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
    }

    /**
     * Find the minimum value in an array.
     * Throws an ArrayIndexOutOfBoundsException if the array is length 0.
     * @param list the source array
     * @return The minimum value
     */
    static public final int min(int[] list) {
        if (list.length == 0) {
            throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX);
        }
        int min = list[0];
        for (int i = 1; i < list.length; i++) {
            if (list[i] < min)
                min = list[i];
        }
        return min;
    }

    /**
     * Find the minimum value in an array.
     * Throws an ArrayIndexOutOfBoundsException if the array is length 0.
     * @param list the source array
     * @return The minimum value
     */
    static public final float min(float[] list) {
        if (list.length == 0) {
            throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX);
        }
        float min = list[0];
        for (int i = 1; i < list.length; i++) {
            if (list[i] < min)
                min = list[i];
        }
        return min;
    }
}

Related

  1. min(int[] array)
  2. min(int[] array)
  3. min(int[] array)
  4. min(int[] elements)
  5. min(int[] in, int x1, int x2)
  6. min(int[] values)
  7. min(int[] values)
  8. min(int[] vs)
  9. min(int[] x, int length)