This utility method checks whether a given array contains any negative value. - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

This utility method checks whether a given array contains any negative value.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] doubleArray = new double[] { 34.45, 35.45, 36.67, 37.78,
                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(isContainNegative(doubleArray));
    }/*from  w ww.j ava 2  s.co  m*/

    /**
     * This utility method checks whether a given array contains any negative
     * value. It returns <tt>true </tt> if it does, <tt>false</tt> if it does
     * not
     * 
     * */
    public static boolean isContainNegative(double[] doubleArray) {
        boolean negativeFound = false;
        int i = 0;
        while (i < doubleArray.length && !negativeFound) {
            if (doubleArray[i] < 0) {
                negativeFound = true;
            } else {
                i++;
            }
        }
        return negativeFound;
    }
}

Related Tutorials