Returns the zero based index of the minimal element of the float array - Java java.lang

Java examples for java.lang:float Array

Description

Returns the zero based index of the minimal element of the float array

Demo Code



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(getIndexOfMinElement(elements));
    }/*from   ww w .  j  av a 2 s. c  o  m*/
    /**
     * Returns the zerobased index of the minimal element of the collection
     * @param elements a collection of floating point numbers
     * @return the index of the smallest element in the collection
     */
    public static int getIndexOfMinElement(Float[] elements) {
        float min = elements[0];
        int index = 0;
        for (int i = 1; i < elements.length; i++) {
            if (elements[i] < min) {
                min = elements[i];
                index = i;
            }
        }
        return index;
    }
}

Related Tutorials