Java Array Min Value minValue(int[] array)

Here you can find the source of minValue(int[] array)

Description

Returns the minimum value in the input array.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

Minimum value

Declaration

public static int minValue(int[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*  w  w  w .j  a v  a 2  s.c  o m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Returns the minimum value in the input array.
     *
     * @param array Input array
     * @return Minimum value
     */
    public static int minValue(int[] array) {
        if (array.length == 0)
            throw new NoSuchElementException("Empty array");

        int minValue = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] < minValue)
                minValue = array[i];

        return minValue;
    }

    /**
     * Returns the minimum value in the input matrix.
     *
     * @param matrix Input matrix
     * @return Minimum value
     */
    public static double minValue(int[][] matrix) {
        if (matrix.length == 0)
            throw new NoSuchElementException("Empty array");

        int minValue = matrix[0][0];
        for (int[] matrix1 : matrix)
            for (int j = 0; j < matrix1.length; j++)
                if (matrix1[j] < minValue)
                    minValue = matrix1[j];

        return minValue;
    }

    /**
     * Returns the minimum value in the input collection.
     *
     * @param collection Input collection
     * @return Minimum value
     *  
     */
    public static int minValue(Collection<Integer> collection) {
        if (collection.isEmpty())
            throw new NoSuchElementException("Empty collection");

        int minValue = Integer.MAX_VALUE;
        for (int value : collection)
            if (value < minValue)
                minValue = value;

        return minValue;
    }

    /**
     * Returns the minimum value in the input map.
     *
     * @param <A> Key type
     * @param map Input map
     * @return Minimum value
     *  
     */
    public static <A> int minValue(Map<A, Integer> map) {
        return minValue(map.values());
    }
}

Related

  1. minValue(double[] from)
  2. minValue(double[] values)
  3. minValue(double[] values)
  4. minValue(final int[] arr)
  5. minValue(float[] arr)
  6. minValue(int[] values)
  7. minValue(long[] array)
  8. minX(final float[] vertices)