Compare two double arrays and return true if both not null, and are of equal length and contain equal values. - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

Compare two double arrays and return true if both not null, and are of equal length and contain equal values.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] a1 = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
                37.1234, 67.2344, 68.34534, 69.87700 };
        double[] a2 = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
                37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(arraysAreEqual(a1, a2));
    }//from w ww .j a  va  2  s .  c o  m

    /**
     * <p>Compare two double arrays and return true if both not null, and are of equal length and contain equal values.</p>
     *
     * @param   a1   first array
     * @param   a2   second array
     * @return      true if equal
     */
    static public boolean arraysAreEqual(double[] a1, double[] a2) {
        //System.err.println("ArrayCopyUtilities.arraysAreEqual(): a1 = "+a1);
        //System.err.println("ArrayCopyUtilities.arraysAreEqual(): a2 = "+a2);
        boolean result = true;
        if (a1 == null || a2 == null || a1.length != a2.length) {
            result = false;
        } else {
            for (int i = 0; i < a1.length; ++i) {
                //if (a1[i] != a2[i]) {
                if (Math.abs(a1[i] - a2[i]) > 0.000001) {
                    result = false;
                    break;
                }
            }
        }
        //System.err.println("ArrayCopyUtilities.arraysAreEqual(): "+result);
        return result;
    }
}

Related Tutorials