Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Get the minimum and maximum values of an array restricting the values to either positive or negative. If <tt>positive</tt> is true,
     * only positive numbers will be addressed. If <tt>positive</tt> is false, only negative numbers will be addressed.
     * 
     * @param array
     *            the array to process
     * @param positive
     *            If true, negative numbers are ignored. If false, positive numbers are ignored.
     * @return a <tt>double[]</tt> where [0]==minimum and [1]==maximum
     */
    public static double[] minMaxSigned(double[] array, boolean positive) {
        double min, max;
        double val;
        if (positive) {
            min = Double.POSITIVE_INFINITY;
            max = 0;
            for (int i = 0; i < array.length; i++) {
                val = array[i];
                if (val >= 0) {
                    min = (val < min) ? val : min;
                    max = (val > max) ? val : max;
                }
            }
        } else {
            min = 0;
            max = Double.NEGATIVE_INFINITY;
            for (int i = 0; i < array.length; i++) {
                val = array[i];
                if (val <= 0) {
                    min = (val < min) ? val : min;
                    max = (val > max) ? val : max;
                }
            }
        }
        return new double[] { min, max };
    }
}