Java Array Max Value max(int[] list)

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

Description

Find the maximum value in an array.

License

Open Source License

Parameter

Parameter Description
list the source array

Return

The maximum value

Declaration

static public final int max(int[] list) 

Method Source Code

//package com.java2s;
/*/*w  w  w  .ja va  2 s .  co  m*/
 * Copyright (c) 2013 maybites.ch
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal 
 *  in the Software without restriction, including without limitation the rights 
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 *  copies of the Software, and to permit persons to whom the Software is furnished
 *  to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class Main {
    static final String ERROR_MIN_MAX = "Cannot use min() or max() on an empty array.";

    static public final int max(int a, int b) {
        return (a > b) ? a : b;
    }

    static public final float max(float a, float b) {
        return (a > b) ? a : b;
    }

    static public final int max(int a, int b, int c) {
        return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
    }

    static public final float max(float a, float b, float c) {
        return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
    }

    /**
     * Find the maximum value in an array.
     * Throws an ArrayIndexOutOfBoundsException if the array is length 0.
     * @param list the source array
     * @return The maximum value
     */
    static public final int max(int[] list) {
        if (list.length == 0) {
            throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX);
        }
        int max = list[0];
        for (int i = 1; i < list.length; i++) {
            if (list[i] > max)
                max = list[i];
        }
        return max;
    }

    /**
     * Find the maximum value in an array.
     * Throws an ArrayIndexOutOfBoundsException if the array is length 0.
     * @param list the source array
     * @return The maximum value
     */
    static public final float max(float[] list) {
        if (list.length == 0) {
            throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX);
        }
        float max = list[0];
        for (int i = 1; i < list.length; i++) {
            if (list[i] > max)
                max = list[i];
        }
        return max;
    }
}

Related

  1. max(int[] as)
  2. max(int[] data)
  3. max(int[] elements)
  4. max(int[] ids)
  5. max(int[] in, int x1, int x2)
  6. max(int[] nums)
  7. max(int[] t)
  8. max(int[] vals, int max)
  9. max(int[] values)