Check if two Boolean Arrays are equal or not in Java
Declaration :
1 |
public static boolean equals(boolean[] a, boolean[] a2) |
Explanation :
Purpose | The java.util.Arrays.equals(boolean[] a boolean[] a2) method returns true if the two specified arrays of booleans 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. |
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 Program : Example
Below example will explain java.util.Arrays.equals() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.c4learn; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // Initialize 4 Boolean Arrays boolean[] arr1 = new boolean[] { false, true, true,false }; boolean[] arr2 = new boolean[] { false, true, false,true }; boolean[] arr3 = new boolean[] { false, true, false,true }; boolean[] arr4 = new boolean[] { false, true, true,false }; // Compare arr1 and arr2 boolean returnV1 = Arrays.equals(arr1, arr2); System.out.println("Is arr1 and arr2 equal : " + returnV1); // Compare arr1 and arr3 boolean returnV2 = Arrays.equals(arr1, arr3); System.out.println("Is arr1 and arr3 equal : " + returnV2); // Compare arr1 and arr4 boolean returnV3 = Arrays.equals(arr1, arr4); System.out.println("Is arr1 and arr4 equal : " + returnV3); // Compare arr2 and arr3 boolean returnV4 = Arrays.equals(arr2, arr3); System.out.println("Is arr2 and arr3 equal : " + returnV4); // Compare arr2 and arr4 boolean returnV5 = Arrays.equals(arr2, arr4); System.out.println("Is arr2 and arr4 equal : " + returnV5); // Compare arr3 and arr4 boolean returnV6 = Arrays.equals(arr3, arr4); System.out.println("Is arr3 and arr4 equal : " + returnV6); } } |
Output of Program :
1 2 3 4 5 6 |
Is arr1 and arr2 equal : false Is arr1 and arr3 equal : false Is arr1 and arr4 equal : true Is arr2 and arr3 equal : true Is arr2 and arr4 equal : false Is arr3 and arr4 equal : false |