Java Array Max Value maxInArray(int[] values)

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

Description

Returns the maximum value in an array of integers.

License

Apache License

Parameter

Parameter Description
values The array to find the amx in.

Return

The max value.

Declaration

public static int maxInArray(int[] values) 

Method Source Code

//package com.java2s;
/*//w  w w .  ja  v a 2  s  . c  om
 * Copyright The Sett Ltd, 2005 to 2014.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Returns the maximum value in an array of integers.
     *
     * @param  values The array to find the amx in.
     *
     * @return The max value.
     */
    public static int maxInArray(int[] values) {
        if ((values == null) || (values.length == 0)) {
            throw new IllegalArgumentException("Cannot find the max of a null or empty array.");
        }

        int max = values[0];

        for (int value : values) {
            max = (max < value) ? value : max;
        }

        return max;
    }
}

Related

  1. maximum(float[] array)
  2. maximumDouble(double... values)
  3. maximumOf(final int[] array)
  4. maxIn(double[] array)
  5. maxInArray(int[] src)
  6. maxIndAbs(double[] a)
  7. maxIndex(double values[])
  8. maxIndex(double[] a)
  9. maxIndex(double[] arr)