Compare Two Java double arrays

Java 8Object Oriented ProgrammingProgramming

To compare two Java double arrays, use the Arrays.equals() method. The following are our double arrays.

double[] arr1 = new double[] { 1.3, 7.2, 4.2 };
double[] arr2 = new double[] { 1.3, 7.2, 4.2 };
double[] arr3 = new double[] { 2.8, 5.3, 9.8 };
double[] arr4 = new double[] { 2.9, 5.8, 7.8 };

Now, let us compare the above arrays. Two of these arrays will be compared at once.

Arrays.equals(arr1, arr2);
Arrays.equals(arr1, arr3);
Arrays.equals(arr1, arr4);

The following is the final example to compare Java double arrays.

Example

 Live Demo

import java.util.*;
public class Demo {
    public static void main(String args[]) {
       double[] arr1 = new double[] { 1.3, 7.2, 4.2 };
       double[] arr2 = new double[] { 1.3, 7.2, 4.2 };
       double[] arr3 = new double[] { 2.8, 5.3, 9.8 };
       double[] arr4 = new double[] { 2.9, 5.8, 7.8 };
       System.out.println(Arrays.equals(arr1, arr2));
       System.out.println(Arrays.equals(arr1, arr3));
       System.out.println(Arrays.equals(arr1, arr4));
       System.out.println(Arrays.equals(arr2, arr3));
       System.out.println(Arrays.equals(arr2, arr4));
    }
}

Output

true
false
false
false
false
raja
Published on 12-Dec-2018 05:16:32
Advertisements