Java Array Max Value max(int[] values)

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

Description

max

License

Open Source License

Declaration

public static int max(int[] values) 

Method Source Code

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

public class Main {
    public static int max(int[] values) {
        return values[max(values, 0, values.length)];
    }//w w w. j a  v a2  s  . c  o  m

    public static int max(int[] values, int begin, int end) {
        int index = maxIndex(values, begin, end);
        if (index >= 0 && index < values.length) {
            return values[index];
        } else {
            return Integer.MIN_VALUE;
        }
    }

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

    public static double max(double[] values, int begin, int end) {
        int index = maxIndex(values, begin, end);
        if (index >= 0 && index < values.length) {
            return values[index];
        } else {
            return Double.NaN;
        }
    }

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

    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. max(int[] list)
  2. max(int[] nums)
  3. max(int[] t)
  4. max(int[] vals, int max)
  5. max(int[] values)
  6. max(int[] values)
  7. max(int[] values)
  8. max(int[] values)
  9. max(int[] vs)