Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised October 21, 2025)

Previous: 7. Array Algorithms

FindMin

//
// FindMin.java
//


import java.util.*;


public class FindMin
{
    public static double findMin(double[] values)
    {
        // find and return the minimum value in a list

        if (values == null || values.length == 0)
            return Double.MAX_VALUE; // or Double.NaN

        double result = values[0];        

        for (double value : values)
        {
            if (value < result)
                result = value;
        }

        return result;
    }

    public static void main(String[] args)
    {
        double[] values = {6, 5, 4, 1, 2, 3};

        System.out.print("values: ");
        for (double x : values)
            System.out.print(x + " ");
        System.out.println();

        System.out.println("min: " + findMin(values));
    }
}

Output:

values: 6.0 5.0 4.0 1.0 2.0 3.0 
min: 1.0

Next: