二维数组等价?

2D Array Equivalence?

我正在尝试确定两个二维数组在整数值方面是否相等。当我为数组输入以下值时:

First Array:
1 1 1 
1 2 2 
1 1 1 

Second Array:
1 1 1 
1 1 1 
1 1 1 

//I get:
Equivalent
//which is not true, since the arrays are not identical.

我不确定我做错了什么。我的这个问题的代码附在下面。任何帮助将不胜感激。

P.S。这是我第一次在 S.O. 上发帖,所以如果我在格式化方面做错了什么,请原谅我并纠正我。谢谢!

import java.util.*;
public class TwoDimensionalArrayEquivalence {

    public static void main(String[] args) {

        //initialize the first two arrays
        int[][] firstArray = new int[3][3];
        int[][] secondArray = new int[3][3];

        Scanner scan = new Scanner(System.in);

        //ask user to input first array
        System.out.println("Please input an array of 9 numbers: ");
        int userInput = 0;
        for (int row = 0; row < firstArray.length; row++) {
            for (int column = 0; column < firstArray.length; column++) {
                userInput = scan.nextInt();
                firstArray[row][column] = userInput;
            }
        }

        //ask the user to input the second array
        System.out.println("\nPlease input another array of 9 numbers: ");
        int userInput2 = 0;
        for (int row = 0; row < secondArray.length; row++) {
            for (int column = 0; column < secondArray.length; column++) {
                userInput2 = scan.nextInt();
                secondArray[row][column] = userInput2;
            }
        }

        //print the first array user has input
        System.out.println("\nFirst Array:");
        for (int row = 0; row < firstArray.length; row++) {
            for (int column = 0; column < firstArray[row].length; column++) {
                System.out.print(firstArray[row][column] + " ");
            }
            System.out.println();
        }

        //print the second array user has input
        System.out.println("\nSecond Array:");
        for (int row = 0; row < secondArray.length; row++) {
            for (int column = 0; column < secondArray[row].length; column++) {
                System.out.print(secondArray[row][column] + " ");
            }
            System.out.println();
        }

        //call method CheckArrayEquality to check for array equivalence
        CheckArrayEquality(firstArray, secondArray);

    }



    public static void CheckArrayEquality(int[][] firstArray, int[][] secondArray){

        boolean decider = false;

        //here, I'm trying to traverse the arrays by incrementing by each row and column
        for (int row = 0; row < firstArray.length; row++) {
            for (int column = 0; column < firstArray[row].length; column++) {
                //Below: if the value in a specific row and column in firstArray is equal to 
                //the value in the same specific row and column in secondArray, then decider is 
                //"true". Else, decider is "false"
                if(firstArray[row][column] == secondArray[row][column]){
                    decider = true;
                }

                else {
                    decider = false;
                }
            }   
        } 

        //if-else statement for printing out whether the arrays are equivalent or not
        //based on what the decider has become
        if (decider == false) {
            System.out.println("\nNot equivalent");
        }
        else if (decider == true) {
            System.out.println("\nEquivalent");
        }
    }
}

实际上,如果矩阵的最后一个整数等于第二个整数,则您的应用会返回 Equivalent。

示例

First Array:
1 1 1 
1 2 2 
1 1 1 

Second Array:
1 1 1 
1 1 1 
1 1 1 

Equivalent



First Array:
1 1 1 
1 2 2 
1 1 1 

Second Array:
1 1 1 
1 1 1 
1 1 2 

Not Equivalent

修复:

public static void CheckArrayEquality(int[][] firstArray, int[][] secondArray){

        boolean decider = false;

        //here, I'm trying to traverse the arrays by incrementing by each row and column
        linesLoop:
        for (int row = 0; row < firstArray.length; row++) {
            for (int column = 0; column < firstArray[row].length; column++) {
                //Below: if the value in a specific row and column in firstArray is equal to
                //the value in the same specific row and column in secondArray, then decider is
                //"true". Else, decider is "false"
                if(firstArray[row][column] == secondArray[row][column]){
                    decider = true;
                }

                else {
                    decider = false;
                    break linesLoop; // Don't check others, we already have a non equal values.
                }
            }
        }

        //if-else statement for printing out whether the arrays are equivalent or not
        //based on what the decider has become
        if (decider == false) {
            System.out.println("\nNot equivalent");
        }
        else if (decider == true) {
            System.out.println("\nEquivalent");
        }
    }

我认为您是在比较==。数组是对象,所以你必须使用 equals。或者更好的是,Arrays.deepEquals() 来自 Arrays 库:

class Test {
    public static void main(String[] args) {
        int[][] arr = {{1, 2}, {3, 4}};
        int[][] arr2 = {{1, 2}, {3, 4}};
        System.out.println(checkequals(arr, arr2));
    }

    private static boolean checkequals(int[][] arr, int[][] arr2) {
        return Arrays.deepEquals(arr, arr2);
    }
}

您在每次迭代中都覆盖 decider 的值,这意味着您最终将获得的唯一值是最后一个条目是否相等。

更好的实施方式是:

public static boolean array2dEquals(int[][] firstArray, int[][] secondArray){
    if(firstArray.length != secondArray.length) return false;
    for (int row = 0; row < firstArray.length; row++) {
        if(firstArray[row].length != secondArray[row].length) return false;
        for (int column = 0; column < firstArray[row].length; column++) {
            if(firstArray[row][column] != secondArray[row][column]) return false;
        }   
    } 
    return true;
}

用法:

if(array2dEquals(firstArray, secondArray)) {
    System.out.println("\nEquivalent");
} else {
    System.out.println("\nNot equivalent");
}

此实现还首先检查数组长度,因此如果数组大小不匹配,您将不会得到任何 ArrayIndexOutOfBoundsException


但是除非你关心每一点性能,否则最好只使用 java 已经提供的:

if(Arrays.deepEquals(firstArray, secondArray)) {
    System.out.println("\nEquivalent");
} else {
    System.out.println("\nNot equivalent");
}