Java Array Max Value max(long[] array)

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

Description

max

License

Open Source License

Declaration

public static long max(long[] array) 

Method Source Code

//package com.java2s;

public class Main {

    public static long max(long[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }//ww  w. ja va 2  s  .com

        // Finds and returns max
        long lMax = array[0];
        for (int j = 1; j < array.length; j++) {
            if (array[j] > lMax) {
                lMax = array[j];
            }
        }

        return lMax;
    }

    public static int max(int[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        int iMax = array[0];
        for (int j = 1; j < array.length; j++) {
            if (array[j] > iMax) {
                iMax = array[j];
            }
        }

        return iMax;
    }

    public static short max(short[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        short sMax = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > sMax) {
                sMax = array[i];
            }
        }

        return sMax;
    }

    public static double max(double[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        double dMax = array[0];
        for (int j = 1; j < array.length; j++) {
            if (array[j] > dMax) {
                dMax = array[j];
            }
        }

        return dMax;
    }

    public static float max(float[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        float fMax = array[0];
        for (int j = 1; j < array.length; j++) {
            if (array[j] > fMax) {
                fMax = array[j];
            }
        }

        return fMax;
    }

    public static long max(long a, long b, long c) {
        long lX = a;
        if (b > lX) {
            lX = b;
        }
        if (c > lX) {
            lX = c;
        }
        return lX;
    }

    public static int max(int a, int b, int c) {
        int iX = a;
        if (b > iX) {
            iX = b;
        }
        if (c > iX) {
            iX = c;
        }
        return iX;
    }

    public static short max(short a, short b, short c) {
        short sX = a;
        if (b > sX) {
            sX = b;
        }
        if (c > sX) {
            sX = c;
        }
        return sX;
    }

    public static byte max(byte a, byte b, byte c) {
        byte byteX = a;
        if (b > byteX) {
            byteX = b;
        }
        if (c > byteX) {
            byteX = c;
        }
        return byteX;
    }

    public static double max(double a, double b, double c) {
        return Math.max(Math.max(a, b), c);
    }

    public static float max(float a, float b, float c) {
        return Math.max(Math.max(a, b), c);
    }
}

Related

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