Java Arrays equals(int[], int[]) Method



Description

The Java Arrays equals(int[] a, int[] a2) method returns true if the two specified arrays of ints 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.

Declaration

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

public static boolean equals(int[] a, int[] 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

Java Arrays equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) Method

Description

The Java Arrays equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) method returns true if the two arrays of ints in the given ranges are equal to each other. In case of any array null, a NullPointerException is thrown.

Declaration

Following is the declaration for java.util.Arrays.compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) method

public static boolean equals​(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

Parameters

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

  • aFromIndex − This is the index of the first element (inclusive) of first array to be tested for equality.

  • aToIndex − This is the index of the last element (exclusive) of first array to be tested for equality.

  • b − This is the second array to be tested for equality.

  • bFromIndex − This is the index of the first element (inclusive) of second array to be tested for equality.

  • bToIndex − This is the index of the last element (exclusive) of second array to be tested for equality.

Return Value

This method returns true if the two arrays, over the specified ranges, are equal.

Exception

  • IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex

  • ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex > a.length or if bFromIndex < 0 or bToIndex > b.length

  • NullPointerException − if either array is null

Checking Arrays of ints for Equality Example

The following example shows the usage of Java Arrays equals(int[], int[]) method. First, we've created three arrays of ints, and compared them using equals(int[], int[]) method. As per the result, the comparison is printed.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing three int arrays
      int[] arr1 = new int[] { 10, 15, 20 };
      int[] arr2 = new int[] { 12, 18, 20 };
      int[] arr3 = new int[] { 10, 15, 20 };

      // 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);
   }
}

Output

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

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

Checking Sub-Arrays of ints for Equality Example

The following example shows the usage of Java Arrays equals(int[], int, int, int[], int, int) method. First, we've created three arrays of ints, and compared them using equals(int[], int, int, int[], int, int) method. As per the result, the comparison is printed.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing three int arrays
      int[] arr1 = new int[] { 10, 15, 20 };
      int[] arr2 = new int[] { 12, 18, 20 };
      int[] arr3 = new int[] { 10, 15, 20 };

      // comparing arr1 and arr2
      boolean retval = Arrays.equals(arr1, 0, 2, arr2, 0, 2);
      System.out.println("arr1 and arr2 equal: " + retval);

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

Output

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

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

Checking Sub-Arrays of ints for Equality Example

The following example shows the usage of Java Arrays equals(int[], int, int, int[], int, int) method. First, we've created three arrays of ints, and compared their sub-arrays using equals(int[], int, int, int[], int, int) method. As per the result, the comparison is printed.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing three int arrays
      int[] arr1 = new int[] { 10, 15, 20 };
      int[] arr2 = new int[] { 12, 18, 20 };
      int[] arr3 = new int[] { 10, 15, 20 };

      // comparing arr1 and arr2
      boolean retval = Arrays.equals(arr1, 2, 3, arr2, 2, 3);
      System.out.println("Last element of arr1 and arr2 equal: " + retval);

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

Output

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

Last element of arr1 and arr2 equal: true
Last element of arr1 and arr3 equal: true
java_util_arrays.htm
Advertisements