Java Arrays - equals() Method
The java.util.Arrays.equals() method is used to check whether the two specified arrays of booleans are equal or not. It returns true if the two arrays are equal, else returns false. The two arrays are considered equal if they contain the same elements in the same order.
Syntax
public static boolean equals(boolean[] a, boolean[] b)
Parameters
a |
Specify the first array to be tested for equality. |
b |
Specify the second array to be tested for equality. |
Return Value
Returns true if the two arrays are equal, else returns false.
Exception
NA.
Example:
In the example below, the java.util.Arrays.equals() method is used to check whether the two arrays of booleans are equal or not.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating three boolean arrays boolean Arr1[] = {true, false, false, true, false}; boolean Arr2[] = {true, false, false, true, false}; boolean Arr3[] = {true, true, true, true, true}; //check Arr1 and Arr2 for equality boolean result1 = Arrays.equals(Arr1, Arr2); System.out.print("Are Arr1 and Arr2 equal?: "+ result1); //check Arr1 and Arr3 for equality boolean result2 = Arrays.equals(Arr1, Arr3); System.out.print("\nAre Arr1 and Arr3 equal?: "+ result2); } }
The output of the above code will be:
Are Arr1 and Arr2 equal?: true Are Arr1 and Arr3 equal?: false
❮ Java.util - Arrays