returns the maximum element of an int array - Java java.lang

Java examples for java.lang:int Array

Description

returns the maximum element of an int array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float[] elements = new float[] { 34.45f, 35.45f, 36.67f, 37.78f,
                37.0000f, 37.1234f, 67.2344f, 68.34534f, 69.87700f };
        System.out.println(getMaxElement(elements));
    }// w ww.  j a va  2 s .com

    public static float getMaxElement(float[] elements) {
        float max = elements[0];
        for (int i = 1; i < elements.length; i++) {
            if (elements[i] > max) {
                max = elements[i];
            }
        }
        return max;
    }

    /**
     * returns the maximum element of an int array
     * @param elements
     * @return
     */
    public static int getMaxElement(int[] elements) {
        int max = elements[0];
        for (int i = 1; i < elements.length; i++) {
            if (elements[i] > max) {
                max = elements[i];
            }
        }
        return max;
    }
}

Related Tutorials