Java Array Max Value argmax(int[] array)

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

Description

argmax

License

LGPL

Declaration

public static final int argmax(int[] array) 

Method Source Code

//package com.java2s;
// terms of the Lesser GNU General Public License (LGPL)

public class Main {
    /**//from www. jav a 2  s . c  om
     * @deprecated
     * @see getArgmax()
    */
    public static final int argmax(int[] array) {
        return getArgmax(array);
    }

    /**
     * @deprecated
     * @see getArgmax()
    */
    public static final int argmax(double[] array) {
        return getArgmax(array);
    }

    /**
     * Find the maximum "argument"
     * @param array The array to examine
     * @return the element of the array with the maximum value
     * @note if array is zero length returns -1
     */
    public static final int getArgmax(int[] array) {
        if (array.length == 0) {
            return -1;
        }
        int maxValue = array[0];
        int maxIndex = 0;
        for (int i = 1; i < array.length; i++) {
            final int v = array[i];
            if (v > maxValue) {
                maxValue = v;
                maxIndex = i;
            }
        }
        return maxIndex;
    }

    /**
     * Find the maximum "argument" (of a double array)
     * @param array The array to examine
     * @return the element of the array with the maximum value
     * @note if array is zero length returns -1
     */
    public static final int getArgmax(double[] array) {
        if (array.length == 0) {
            return -1;
        }
        double maxValue = array[0];
        int maxIndex = 0;
        for (int i = 1; i < array.length; i++) {
            final double v = array[i];
            if (v > maxValue) {
                maxValue = v;
                maxIndex = i;
            }
        }
        return maxIndex;
    }
}

Related

  1. argmax(double[] weights)
  2. argmax(final double[] vec)
  3. argmax(float[] args)
  4. argMax(int[] a)
  5. argmax(int[] a)
  6. argmax(int[] array)
  7. argmax(int[] input)
  8. arrayMax(double maxVal, double[] vals)
  9. arrayMax(double[] arr)