Java Array Min Value minIndex(int[] values)

Here you can find the source of minIndex(int[] values)

Description

min Index

License

Open Source License

Declaration

public static int minIndex(int[] values) 

Method Source Code

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

public class Main {
    public static int minIndex(int[] values) {
        return minIndex(values, 0, values.length);
    }//from w ww  .ja va 2 s .  c o  m

    public static int minIndex(int[] values, int begin, int end) {
        int min = Integer.MAX_VALUE;
        int index = -1;
        if (checkArguments(values, begin, end)) {
            min = values[begin];
            index = begin;
            for (int i = begin; i < end; i++) {
                if (values[i] < min) {
                    min = values[i];
                    index = i;
                }
            }
        }
        return index;
    }

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

    public static int minIndex(double[] values, int begin, int end) {
        double min = Double.NaN;
        int index = -1;
        if (checkArguments(values, begin, end)) {
            min = values[begin];
            index = begin;
            for (int i = begin; i < end; i++) {
                if (!Double.isNaN(values[i])) {
                    if (values[i] < min) {
                        min = 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. minIndex(float[] vals)
  2. minIndex(float[] xs)
  3. minIndex(int[] array)
  4. minIndex(int[] from)
  5. minIndex(int[] ints)
  6. minIndex(Number[] array)
  7. minIndexGreaterThan(final double[] array, final double p)
  8. minInRowIndex(double[][] M, int row)
  9. minInt(int... values)