Java Array Max Value max(Number[] array)

Here you can find the source of max(Number[] array)

Description

Returns the (first occurrence of the) biggest value in the given array.

License

Open Source License

Parameter

Parameter Description
array the array to work on

Return

the biggest value

Declaration

public static Number max(Number[] array) 

Method Source Code

//package com.java2s;
/*/*from w w  w . j  a  v a  2 s. com*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns the (first occurrence of the) biggest value in the given array.
     * Null in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the biggest value
     */
    public static Number max(Number[] array) {
        int index;

        index = maxIndex(array);

        if (index == -1)
            return null;
        else
            return array[index];
    }

    /**
     * Returns the (first occurrence of the) biggest value in the given array.
     * Integer.MAX_VALUE in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the biggest value
     */
    public static int max(int[] array) {
        Integer result;

        result = (Integer) max(toNumberArray(array));
        if (result == null)
            return Integer.MAX_VALUE;
        else
            return result;
    }

    /**
     * Returns the (first occurrence of the) biggest value in the given array.
     * Double.MAX_VALUE in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the biggest value
     */
    public static double max(double[] array) {
        Double result;

        result = (Double) max(toNumberArray(array));
        if (result == null)
            return Double.MAX_VALUE;
        else
            return result;
    }

    /**
     * Returns the (first occurrence of the) index of the cell with the biggest
     * number. -1 in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the index
     */
    public static int maxIndex(Number[] array) {
        int result;
        int i;
        double maxValue;

        result = -1;

        maxValue = -Double.MAX_VALUE;
        for (i = 0; i < array.length; i++) {
            if (array[i].doubleValue() > maxValue) {
                maxValue = array[i].doubleValue();
                result = i;
            }
        }

        return result;
    }

    /**
     * Returns the (first occurrence of the) index of the cell with the biggest
     * int. -1 in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the index
     */
    public static int maxIndex(int[] array) {
        return maxIndex(toNumberArray(array));
    }

    /**
     * Returns the (first occurrence of the) index of the cell with the biggest
     * double. -1 in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the index
     */
    public static int maxIndex(double[] array) {
        return maxIndex(toNumberArray(array));
    }

    /**
     * Turns the byte array into a Byte array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(byte[] array) {
        Byte[] result;
        int i;

        result = new Byte[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Byte(array[i]);

        return result;
    }

    /**
     * Turns the short array into a Short array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(short[] array) {
        Short[] result;
        int i;

        result = new Short[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Short(array[i]);

        return result;
    }

    /**
     * Turns the int array into a Integer array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(int[] array) {
        Integer[] result;
        int i;

        result = new Integer[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Integer(array[i]);

        return result;
    }

    /**
     * Turns the long array into a Long array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(long[] array) {
        Long[] result;
        int i;

        result = new Long[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Long(array[i]);

        return result;
    }

    /**
     * Turns the float array into a Float array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(float[] array) {
        Float[] result;
        int i;

        result = new Float[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Float(array[i]);

        return result;
    }

    /**
     * Turns the double array into a Double array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(double[] array) {
        Double[] result;
        int i;

        result = new Double[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Double(array[i]);

        return result;
    }
}

Related

  1. max(Long john, Long... jane)
  2. max(long[] array)
  3. max(long[] array)
  4. max(long[] array)
  5. max(Number... array)
  6. max(T first, T... others)
  7. max(T... elems)
  8. max(T... values)
  9. max(T[] args)