Java Array Max Value maxIndex(final double[] values)

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

Description

Finds the maximum 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 maximum value in the provided array of doubles.

Declaration

public static int maxIndex(final double[] values) 

Method Source Code

//package com.java2s;
/*//w  w w . j  a v a2s.  c o  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 maximum 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 maximum value in the provided array of doubles.
     */
    public static int maxIndex(final double[] values) {
        if ((values != null) && (values.length != 0)) {
            double max = 0;
            int maxIndex = -1;
            for (int i = 0; i < values.length; i++) {
                if (values[i] > max) {
                    max = values[i];
                    maxIndex = i;
                }
            }
            return maxIndex;
        } else {
            throw new IllegalArgumentException("cannot calculate maximum index of null or empty array of values");
        }
    }

    /**
     * Finds the maximum 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 maximum value in the provided array of ints.
     */
    public static int maxIndex(final int[] values) {
        if ((values != null) && (values.length != 0)) {
            int max = 0;
            int maxIndex = -1;
            for (int i = 0; i < values.length; i++) {
                if (values[i] > max) {
                    max = values[i];
                    maxIndex = i;
                }
            }
            return maxIndex;
        } else {
            throw new IllegalArgumentException("cannot calculate maximum index of null or empty array of values");
        }
    }
}

Related

  1. maxIndex(double[] doubles, int begin, int end)
  2. maxIndex(double[] v)
  3. maxIndex(double[] value)
  4. maxIndex(double[] x, int length)
  5. maxIndex(final double[] a)
  6. maxIndex(float[] arr)
  7. maxIndex(float[] from, int start)
  8. maxIndex(float[] x)
  9. maxIndex(int[] a)