📜  比较两个布尔数组的Java程序

📅  最后修改于: 2022-05-13 01:54:20.264000             🧑  作者: Mango

比较两个布尔数组的Java程序

如果两个数组包含相同顺序的相同元素,则它们相等。在Java中,我们可以通过两种方式比较两个布尔数组:

  • 通过使用Java内置方法,即.equals()方法。
  • 通过使用 Naive 方法。

例子:

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.

方法一:

Java中的数组类提供了Arrays.equals()方法来检查两个数组是否相等。

句法:

public static boolean equals(boolean[] a, boolean[] a1)

参数:

a - one array to be tested for equality
a1 - another array to be tested for equality

返回:如果两个数组相等则返回真,否则返回假。

代码:

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


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


输出
Array1...
true
true
false
Array2...
true
true
false
Both the arrays equal

方法二:

  • 在此,我们将使用Naive 方法来比较两个数组。
  • 我们可以运行一个 for 循环并检查数组的每个元素并比较每个元素。

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 ");
    }
}
输出
Array1...
true
true
false
Array2...
true
true
false
Both the arrays equal