Compare Two Boolean Arrays in Java
Use Arrays.equals() method to compare two Boolean Arrays. Let us first create some arrays to be compared. For using this method, you need to import the following package −
import java.util.Arrays;
We have 4 arrays and value “true” and “false” are assigned to them.
boolean[] myArr1 = new boolean[] { true, false, true, true, true }; boolean[] myArr2 = new boolean[] { true, false, true, true , true }; boolean[] myArr3 = new boolean[] { false, false, true, true, true }; boolean[] myArr4 = new boolean[] { true, false, false, true , false };
Now, let us compare Boolean arrays 1 and 2.
Arrays.equals(myArr1, myArr2);
In the same way, we can compare other arrays as well.
Let us see the complete example to compare Boolean Arrays in Java.
Example
import java.util.Arrays; public class Demo { public static void main(String[] args) { boolean[] myArr1 = new boolean[] { true, false, true, true, true }; boolean[] myArr2 = new boolean[] { true, false, true, true , true }; boolean[] myArr3 = new boolean[] { false, false, true, true, true }; boolean[] myArr4 = new boolean[] { true, false, false, true , false }; System.out.println(Arrays.equals(myArr1, myArr2)); System.out.println(Arrays.equals(myArr2, myArr3)); System.out.println(Arrays.equals(myArr3, myArr4)); System.out.println(Arrays.equals(myArr1, myArr4)); } }
Output
true false false false
Advertisements