📌  相关文章
📜  Java程序检查两个数组是否相等

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

Java程序检查两个数组是否相等

给定两个等长的给定数组,任务是找出给定的数组是否相等。如果两个数组都包含相同的元素集且顺序相同,则称这两个数组相等。

注意:如果有重复,那么两个数组的重复元素计数应该相同。

例子:

Input  : arr1[] = {1, 2, 5, 4, 0};
         arr2[] = {1, 2, 5, 4, 0}; 
Output : Yes

Input  : arr1[] = {1, 2, 5, 4, 0, 2};
         arr2[] = {2, 4, 5, 0}; 
Output : No
 
Input : arr1[] = {1, 7, 7};
        arr2[] = {7, 7, 1};
Output : No

方法一:使用预定义的方法

  • 首先,我们将初始化两个数组并将元素插入这两个数组中。
  • 然后调用 Arrays.equal()函数检查两个数组是否相等,并将结果存入一个布尔变量result中。
  • 最后,将打印结果。

示例:下面是上述方法的实现。

Java
// Java Program to find the if the arrays are equal
  
import java.util.Arrays;
  
public class CheckArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 30, 25, 40 };
  
        // Initializing the second array
        int b[] = { 30, 25, 40 };
  
        // store the result
        // Arrays.equals(a, b) function is used to check
        // whether two arrays are equal or not
        boolean result = Arrays.equals(a, b);
  
        // condition to check whether the
        // result is true or false
        if (result == true) {
            // Print the result
            System.out.println("Two arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Two arrays are not equal");
        }
    }
}


Java
// Java Program to find the if the arrays are equal
  
import java.util.Arrays;
  
public class CheckArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 30, 25, 40, 23 };
  
        // Initializing the second array
        int b[] = { 30, 26, 40 };
  
        // store the result
        // Arrays.equals(a, b) function is used to check
        // whether two arrays are equal or not
        boolean result = Arrays.equals(a, b);
  
        // condition to check whether the
        // result is true or false
        if (result == true) {
            // Print the result
            System.out.println("Two arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Two arrays are not equal");
        }
    }
}


Java
// Java Program to check if the arrays are equal
  
public class checkArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 10, 30, 12 };
        // Initializing the second array
        int b[] = { 10, 30, 12 };
  
        // store the result
        boolean result = true;
  
        // Check if length of the two arrays are equal or
        // not
        if (a.length == b.length) {
            
            // Loop to check elements of arrays one by one
            for (int i = 0; i < a.length; i = i + 1) {
                
                // To check if any element is different
                if (a[i] != b[i]) {
                    
                    // If any element is different then it
                    // will assign false into boolean
                    // variable
                    result = false;
                }
            }
        }
        else {
            
            // If the length of two arrays is
            // different then it will assign
            // false into boolean variable
            result = false;
        }
        
        // After completion to check whether
        // result is true of false
        if (result == true) {
            
            // Print the result
            System.out.println("Arrays are equal");
        }
        else {
            
            // Print the result
            System.out.println("Arrays are not equal");
        }
    }
}


Java
// Java Program to check if the arrays are equal
  
public class checkArraysEqual {
    
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 10, 30, 12 };
        // Initializing the second array
        int b[] = { 45, 50, 55, 60, 65 };
  
        // stores the result
        boolean result = true;
  
        // Check if length of the two arrays are equal or
        // not
        if (a.length == b.length) {
            
            // Loop to check elements of arrays one by one
            for (int i = 0; i < a.length; i = i + 1) {
                
                // To check if any element is different
                if (a[i] != b[i]) {
                    
                    // If any element is different then it
                    // will assign false into boolean
                    // variable
                    result = false;
                }
            }
        }
        else {
            
            // If the length of two arrays is different then
            // it will assign false into boolean variable
            result = false;
        }
        
        // After completion to check whether result is true
        // of false
        if (result == true) {
            
            // Print the result
            System.out.println("Arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Arrays are not equal");
        }
    }
}


输出
Two arrays are equal

示例 2:

Java

// Java Program to find the if the arrays are equal
  
import java.util.Arrays;
  
public class CheckArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 30, 25, 40, 23 };
  
        // Initializing the second array
        int b[] = { 30, 26, 40 };
  
        // store the result
        // Arrays.equals(a, b) function is used to check
        // whether two arrays are equal or not
        boolean result = Arrays.equals(a, b);
  
        // condition to check whether the
        // result is true or false
        if (result == true) {
            // Print the result
            System.out.println("Two arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Two arrays are not equal");
        }
    }
}
输出
Two arrays are not equal

时间复杂度: O(n)
辅助空间: O(1)

方法二:不使用预定义函数

  • 首先,我们将初始化两个数组ab并将元素插入这两个数组中。然后创建一个名为result的布尔变量来存储检查后的结果。
  • 然后我们将检查数组的长度是否相等。
  • 如果相等,则循环将对每个元素重复,直到数组结束。如果某处某个元素不相等,那么我们将使结果为false
  • 如果结果变量的值为false ,则表示数组不相等。
  • 如果数组相等,则结果变量的值将保持为真。
  • 而且如果数组的长度不相等,它将返回 false。

示例 1 :下面是上述方法的实现。

Java

// Java Program to check if the arrays are equal
  
public class checkArraysEqual {
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 10, 30, 12 };
        // Initializing the second array
        int b[] = { 10, 30, 12 };
  
        // store the result
        boolean result = true;
  
        // Check if length of the two arrays are equal or
        // not
        if (a.length == b.length) {
            
            // Loop to check elements of arrays one by one
            for (int i = 0; i < a.length; i = i + 1) {
                
                // To check if any element is different
                if (a[i] != b[i]) {
                    
                    // If any element is different then it
                    // will assign false into boolean
                    // variable
                    result = false;
                }
            }
        }
        else {
            
            // If the length of two arrays is
            // different then it will assign
            // false into boolean variable
            result = false;
        }
        
        // After completion to check whether
        // result is true of false
        if (result == true) {
            
            // Print the result
            System.out.println("Arrays are equal");
        }
        else {
            
            // Print the result
            System.out.println("Arrays are not equal");
        }
    }
}
输出
Arrays are equal

示例 2:

Java

// Java Program to check if the arrays are equal
  
public class checkArraysEqual {
    
    public static void main(String[] args)
    {
        // Initializing the first array
        int a[] = { 10, 30, 12 };
        // Initializing the second array
        int b[] = { 45, 50, 55, 60, 65 };
  
        // stores the result
        boolean result = true;
  
        // Check if length of the two arrays are equal or
        // not
        if (a.length == b.length) {
            
            // Loop to check elements of arrays one by one
            for (int i = 0; i < a.length; i = i + 1) {
                
                // To check if any element is different
                if (a[i] != b[i]) {
                    
                    // If any element is different then it
                    // will assign false into boolean
                    // variable
                    result = false;
                }
            }
        }
        else {
            
            // If the length of two arrays is different then
            // it will assign false into boolean variable
            result = false;
        }
        
        // After completion to check whether result is true
        // of false
        if (result == true) {
            
            // Print the result
            System.out.println("Arrays are equal");
        }
        else {
            // Print the result
            System.out.println("Arrays are not equal");
        }
    }
}
输出
Arrays are not equal

时间复杂度: O(n)
辅助空间: O(1)