get Absolute Minimum integer - Java java.lang

Java examples for java.lang:int

Description

get Absolute Minimum integer

Demo Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static int getAbsoluteMinimum(int[] inputArray) {

        if (inputArray == null || inputArray.length == 0)
            throwException();/*ww w. j  av  a2s . c  o m*/

        BigDecimal[] absoluteValues = new BigDecimal[inputArray.length];

        for (int i = 0; i < inputArray.length; i++) {
            absoluteValues[i] = new BigDecimal(inputArray[i]);
        }

        return getAbsoluteMinimum(absoluteValues).intValue();

    }

    public static BigDecimal getAbsoluteMinimum(BigDecimal[] inputArray) {

        if (inputArray == null || inputArray.length == 0)
            throwException();

        BigDecimal[] absoluteValues = new BigDecimal[inputArray.length];

        for (int i = 0; i < inputArray.length; i++) {
            absoluteValues[i] = inputArray[i].abs();
        }

        for (int i = 1; i < absoluteValues.length; i++) {
            if (absoluteValues[0].compareTo(absoluteValues[i]) > 0) {
                BigDecimal t = absoluteValues[0];
                absoluteValues[0] = absoluteValues[i];
                absoluteValues[i] = t;
            }
        }

        //      Arrays.sort(absoluteValues);

        return absoluteValues[0];
    }

    private static void throwException() {
        throw new IllegalArgumentException("Must have a valid input");

    }
}

Related Tutorials