Java.util.Arrays.equals(double[], double[]) Method

Advertisements


Description

The java.util.Arrays.equals(double[] a, double[] a2) method returns true if the two specified arrays of doubles are equal to one another.Two arrays are equal if they contain the same elements in the same order.Two array references are considered equal if both are null.

Two doubles d1 and d2 are considered equal if:
     new Double(d1).equals(new Double(d2))

Declaration

Following is the declaration for java.util.Arrays.equals() method

public static boolean equals(double[] a, double[] a2)

Parameters

  • a -- This is the array to be tested for equality.

  • a2 -- This is the other array to be tested for equality.

Return Value

This method returns true if the two arrays are equal, else false.

Exception

  • NA

Example

The following example shows the usage of java.util.Arrays.equals() method.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {

  public static void main(String[] args) {
    
    // initializing three double arrays
    double[] arr1 = new double[] { 1.2, 1.23 };
    double[] arr2 = new double[] { 1.2, 1.23 };
    double[] arr3 = new double[] { 1.23, 1.2 };
    
    // comparing arr1 and arr2
    boolean retval=Arrays.equals(arr1, arr2);
    System.out.println("arr1 and arr2 equal: " + retval);

    // comparing arr1 and arr3
    boolean retval2=Arrays.equals(arr1, arr3);
    System.out.println("arr1 and arr3 equal: " + retval2);
  }
} 

Let us compile and run the above program, this will produce the following result:

arr1 and arr2 equal: true
arr1 and arr3 equal: false



Advertisements
Advertisements