Java Array Min Value minIndex(final double[] values)

Here you can find the source of minIndex(final double[] values)

Description

Finds the minimum value of an array of double and returns its index in the array.

License

Open Source License

Parameter

Parameter Description
values an array of double.

Return

the index of the minimum value in the provided array of doubles.

Declaration

public static int minIndex(final double[] values) 

Method Source Code

//package com.java2s;
/*//from w w w  . jav  a2s. co m
 * Copyright 2007-2010 Tom Castle & Lawrence Beadle
 * Licensed under GNU General Public License
 * 
 * This file is part of EpochX: genetic programming software for research
 * 
 * EpochX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * EpochX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with EpochX. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The latest version is available from: http:/www.epochx.org
 */

public class Main {
    /**
     * Finds the minimum value of an array of <code>double</code> and returns
     * its index in the array. The provided argument must not be null nor an
     * empty array.
     * 
     * @param values an array of double.
     * @return the index of the minimum value in the provided array of doubles.
     */
    public static int minIndex(final double[] values) {
        if ((values != null) && (values.length != 0)) {
            double min = Double.MAX_VALUE;
            int minIndex = -1;
            for (int i = 0; i < values.length; i++) {
                if (values[i] < min) {
                    min = values[i];
                    minIndex = i;
                }
            }
            return minIndex;
        } else {
            throw new IllegalArgumentException("cannot calculate minimum index of null or empty array of values");
        }
    }

    /**
     * Finds the minimum value of an array of <code>int</code> and returns
     * its index in the array. The provided argument must not be null nor an
     * empty array.
     * 
     * @param values an array of int.
     * @return the index of the minimum value in the provided array of ints.
     */
    public static int minIndex(final int[] values) {
        if ((values != null) && (values.length != 0)) {
            int min = Integer.MAX_VALUE;
            int minIndex = -1;
            for (int i = 0; i < values.length; i++) {
                if (values[i] < min) {
                    min = values[i];
                    minIndex = i;
                }
            }
            return minIndex;
        } else {
            throw new IllegalArgumentException("cannot calculate minimum index of null or empty array of values");
        }
    }
}

Related

  1. minIndex(double[] v)
  2. minIndex(double[] value)
  3. minIndex(double[] x, int length)
  4. minIndex(double[][] matrix, int column)
  5. minIndex(final double[] array)
  6. minIndex(float[] arr)
  7. minIndex(float[] vals)
  8. minIndex(float[] xs)
  9. minIndex(int[] array)