Java Array Max Value maxIndex(int[] from)

Here you can find the source of maxIndex(int[] from)

Description

max Index

License

Apache License

Declaration

public static int maxIndex(int[] from) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /** Returns the index of the largest value in the array.
     * In case of a tie, an the index is selected randomly.
     *//*from  w  ww  .j  a  v a 2  s  .c  om*/
    public static int maxIndex(int[] from, Random rand) {
        assert rand != null;
        int result = 0;
        int maxCount = 0; // count of maximal element for a 1 item reservoir sample
        for (int i = 1; i < from.length; ++i) {
            if (from[i] > from[result]) {
                result = i;
                maxCount = 1;
            } else if (from[i] == from[result]) {
                if (rand.nextInt(++maxCount) == 0)
                    result = i;
            }
        }
        return result;
    }

    public static int maxIndex(float[] from, Random rand) {
        assert rand != null;
        int result = 0;
        int maxCount = 0; // count of maximal element for a 1 item reservoir sample
        for (int i = 1; i < from.length; ++i) {
            if (from[i] > from[result]) {
                result = i;
                maxCount = 1;
            } else if (from[i] == from[result]) {
                if (rand.nextInt(++maxCount) == 0)
                    result = i;
            }
        }
        return result;
    }

    public static int maxIndex(int[] from) {
        int result = 0;
        for (int i = 1; i < from.length; ++i)
            if (from[i] > from[result])
                result = i;
        return result;
    }

    public static int maxIndex(long[] from) {
        int result = 0;
        for (int i = 1; i < from.length; ++i)
            if (from[i] > from[result])
                result = i;
        return result;
    }

    public static int maxIndex(float[] from) {
        int result = 0;
        for (int i = 1; i < from.length; ++i)
            if (from[i] > from[result])
                result = i;
        return result;
    }
}

Related

  1. maxIndex(float[] arr)
  2. maxIndex(float[] from, int start)
  3. maxIndex(float[] x)
  4. maxIndex(int[] a)
  5. maxIndex(int[] array)
  6. maxIndex(int[] list)
  7. maxIndex(int[] shape)
  8. maxIndex(int[] values, int begin, int end)
  9. maxIndex(Number[] array)