Java Program to Compare two Boolean Arrays
Last Updated :
17 Dec, 2020
Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways:
- By using Java built-in method that is .equals() method.
- By using the Naive approach.
Examples:
Input : A = [true , true , false]
A1 = [true, true, false]
Output: Both the arrays are equal.
Input : A = [true, true, false]
A1 = [true, false, true]
Output: Both the arrays are not equal.
Method 1:
Array class in Java provides the method Arrays.equals() to check whether two arrays are equal or not.
Syntax:
public static boolean equals(boolean[] a, boolean[] a1)
Parameters:
a - one array to be tested for equality
a1 - another array to be tested for equality
Returns: It returns true if both the arrays are equal else returns false.
Code:
Java
// Java Program to Compare two Boolean
// Arrays using built-in function
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
// initializing both the boolean arrays
boolean[] a = new boolean[] { true, true, false };
boolean[] a1 = new boolean[] { true, true, false };
// Displaying Array1
System.out.println("Array1...");
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
// Displaying Array2
System.out.println("Array2...");
for (int j = 0; j < a1.length; j++)
{
System.out.println(a1[j]);
}
// comparing array1 and array2
boolean result = Arrays.equals(a, a1);
if (result)
{
System.out.println("Both the arrays equal ");
}
else
{
System.out.println(
"Both the arrays not equal ");
}
}
}
OutputArray1...
true
true
false
Array2...
true
true
false
Both the arrays equal
Method 2:
- In this, we will use the Naive approach to compare two arrays.
- We can run a for loop and check every element of the array and compare each one of them.
Java
// Java Program to Compare two Boolean
// Arrays using Naive approach
import java.io.*;
class GFG {
public static void main(String[] args)
{
// initializing both the boolean arrays
boolean[] a = new boolean[] { true, true, false };
boolean[] a1 = new boolean[] { true, true, false };
// Displaying Array1
System.out.println("Array1...");
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
// Displaying Array2
System.out.println("Array2...");
for (int j = 0; j < a1.length; j++)
{
System.out.println(a1[j]);
}
// Comparing both the arrays
for (int i = 0; i < a.length; i++)
{
// if any element is found different we will
// print our ans and exit the program.
if (a[i] != a1[i])
{
System.out.println(
"Both the arrays equal ");
System.exit(0);
}
}
System.out.println("Both the arrays equal ");
}
}
OutputArray1...
true
true
false
Array2...
true
true
false
Both the arrays equal
Similar Reads
Java Program to convert integer to boolean Given a integer value, the task is to convert this integer value into a boolean value in Java. Examples: Input: int = 1 Output: true Input: int = 0 Output: false Approach: Get the boolean value to be converted. Check if boolean value is true or false If the integer value is greater than equal to 1,
2 min read
Java Program to Convert String to boolean In Java, to convert a string to a Boolean, we can use Boolean.parseBoolean(string) to convert a string to a primitive Boolean, or Boolean.valueOf(string) to convert it to a Boolean object. The Boolean data type only holds two possible values which are true and false. If the string equals "true" (ign
3 min read
Java Program to Find Common Elements Between Two Arrays Given two arrays and our task is to find their common elements. Examples:Input: Array1 = ["Article", "for", "Geeks", "for", "Geeks"], Array2 = ["Article", "Geeks", "Geeks"]Output: [Article, Geeks]Input: Array1 = ["a", "b", "c", "d", "e", "f"], Array2 = ["b", "d", "e", "h", "g", "c"]Output: [b, c, d,
4 min read
Java Program to Check if Two of Three Boolean Variables are True Boolean values are true/false. In this program, we need to check if any two out of three boolean values are true or not. We will be discussing two major approaches to do it: using an if-else.using the ternary operator. Approach 1: Using if-else condition We initialize three boolean variable flags, w
3 min read
Comparing two ArrayList In Java Java provides a method for comparing two Array List. The ArrayList.equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Example: Input : ArrayLis
2 min read