Java Array Max Value maxIndex(float[] arr)

Here you can find the source of maxIndex(float[] arr)

Description

Returns the index to the smallest value in the array

License

Open Source License

Parameter

Parameter Description
arr array of floats

Return

the index

Declaration

public static int maxIndex(float[] arr) 

Method Source Code

//package com.java2s;

public class Main {
    /**/* ww  w. ja  v a  2  s.c o  m*/
     * Returns the index to the smallest value in the array
     * 
     * @param arr array of floats
     * @return the index
     */
    public static int maxIndex(float[] arr) {
        float max = Float.MIN_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
        }

        return index;
    }

    /**
     * Returns the index to the smallest value in the array
     * 
     * @param arr array of ints
     * @return the index
     */
    public static int maxIndex(int[] arr) {
        int max = Integer.MIN_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
        }

        return index;
    }

    /**
     * Returns the index to the smallest value in the array
     * 
     * @param arr array of longs
     * @return the index
     */
    public static int maxIndex(long[] arr) {
        long max = Long.MIN_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
        }

        return index;
    }

    /**
     * Returns the index to the smallest value in the array
     * 
     * @param arr array of bytes
     * @return the index
     */
    public static int maxIndex(byte[] arr) {
        long max = Byte.MIN_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
        }

        return index;
    }

    /**
     * Returns the index to the smallest value in the array
     * 
     * @param arr array of shorts
     * @return the index
     */
    public static int maxIndex(short[] arr) {
        short max = Short.MIN_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
        }

        return index;
    }

    /**
     * Returns the index to the smallest value in the array
     * 
     * @param arr array of doubles
     * @return the index
     */
    public static int maxIndex(double[] arr) {
        double max = -Double.MAX_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
        }

        return index;
    }
}

Related

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