Java Array Max Value maxIndex(int[] values, int begin, int end)

Here you can find the source of maxIndex(int[] values, int begin, int end)

Description

max Index

License

Open Source License

Declaration

public static int maxIndex(int[] values, int begin, int end) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int maxIndex(int[] values) {
        return maxIndex(values, 0, values.length);
    }/*from   w w  w . j  av a2s  .  c om*/

    public static int maxIndex(int[] values, int begin, int end) {
        int max = Integer.MIN_VALUE;
        int index = -1;
        if (checkArguments(values, begin, end)) {
            max = values[begin];
            index = begin;
            for (int i = begin; i < end; i++) {
                if (values[i] > max) {
                    max = values[i];
                    index = i;
                }
            }
        }
        return index;
    }

    public static int maxIndex(double[] values) {
        return maxIndex(values, 0, values.length);
    }

    public static int maxIndex(double[] values, int begin, int end) {
        double max = Double.NaN;
        int index = -1;
        if (checkArguments(values, begin, end)) {
            max = values[begin];
            index = begin;
            for (int i = begin; i < end; i++) {
                if (!Double.isNaN(values[i])) {
                    if (values[i] > max) {
                        max = values[i];
                        index = i;
                    }
                }
            }
        }
        return index;
    }

    private static boolean checkArguments(final int[] values, final int begin, final int end) {
        if (values == null || values.length == 0) {
            return false;
        }
        if (begin < 0 || end >= values.length) {
            return false;
        }
        return true;
    }

    private static boolean checkArguments(final double[] values, final int begin, final int end) {
        if (values == null || values.length == 0) {
            return false;
        }
        if (begin < 0 || end > values.length) {
            return false;
        }
        return true;
    }
}

Related

  1. maxIndex(int[] a)
  2. maxIndex(int[] array)
  3. maxIndex(int[] from)
  4. maxIndex(int[] list)
  5. maxIndex(int[] shape)
  6. maxIndex(Number[] array)
  7. maxIndex(T[] array)
  8. maxIndices(double[] aArray, Integer[] indices)
  9. maxIndices(int values[])