比较不同对象类型的二维数组

时间:2021-01-15 22:23:57

标签: java arrays for-loop multidimensional-array comparison

我编写了一个方法来比较不同的二维数组并使用 .equals 输出它们是否相同。在我的主要测试器类中,我将两个相同的 2D 整数数组相互比较,因此输出应为“真”,然后是两个不同的 2D 字符串数组,因此输出应为假。当测试不同的组合时,当我测试相等和不相等的整数数组时,我会得到正确的输出。测试不同大小的数组、正确的输出和测试相等的字符串数组我得到了正确的输出。

我遇到的问题是,当我测试两个不相等的 String 数组时,程序在它们不相等时返回 true,因此输出应该为 false。在此先感谢您提供任何帮助或提示。

public boolean equals(Object[][] other) {
    boolean isEqual = false;

    if (myArray.length != other.length) {
        return isEqual;
    }

    for (int i = 0; i < myArray.length; i++) {
        for (int j = 0; j < myArray[i].length; j++) {
            if (myArray[i][j].equals(other[i][j])) {
                isEqual = true;
            } else {
                isEqual = false;
            }
        }
    }
    return isEqual;
}

测试:

public class TwoDTester {
    public static void main(String[] args) {
        //Initializing arrays
        Integer[][] firstArray = {{2, 3}, {3, 4}, {4, 5}};
        Integer[][] secondArray = {{2, 3}, {3, 4}, {4, 5}};

        //Creating TwoDArray object for comparisons
        TwoDArray first = new TwoDArray(firstArray);

        //Testing true or false
        System.out.println(first.equals(secondArray));

        //Initializing more arrays
        String[][] thirdArray = {
                {"Hello", "Goodbye"},
                {"Hola", "Adios"},
                {"Bonjour", "Au revoir"}};

        String[][] fourthArray = {
                {"Hello", "Goodbye"},
                {"Ciao", "Addio"},
                {"Bonjour", "Au revoir"}};

        //Creating TwoDArray object for comparisons
        TwoDArray third = new TwoDArray(thirdArray);

        //Testing true or false
        System.out.println(third.equals(fourthArray));
    }
}

2 个答案:

答案 0 :(得分:1)

由于您要求数组中的每个对应元素都相等,因此您应该在发现不相等时退出循环。例如,在这种情况下,您可以 return false;,而仅在循环结束时才返回 true(这意味着所有相应的元素都相等)。

使用您当前的代码,只考虑最后一个元素(无论最后两个元素是否相等,这都会覆盖 isEqual),而不会考虑所有先前的值。

此外,如果您允许 myArray 包含 null 元素,则考虑使用 Objects.equals 方法来比较对象的相等性,但这只是一个提示,与直接问题。

pointed out by @Jems,还应注意以下几行:

if (myArray.length != other.length)
{
    return isEqual;
}

您仅测试每个二维数组具有的行数是否相等。请注意,您可能还应该比较每个数组的每一行以确保其列之间的相等性。例如类似:

if (myArray[i].length != other[i].length)
    return false;

更不用说应该检查 null 数组本身(即 myArraymyArray[i]otherother[i])。

答案 1 :(得分:0)

您可以获取 Arrays.equals 方法的代码并对其进行自定义,使其接受二维数组:

public static void main(String[] args) {
    Integer[][] one = {{1, 2}, {3, 4}, {5, 6}, null};
    Integer[][] two = {{1, 2}, {3, 4}, {5, 6}, null};

    String[][] three = {{"A", "B"}, {"C", "D"}, {"E", null}};
    String[][] four = {{"A", "B"}, {"C", "D"}, {"E", null}};

    System.out.println(equals2D(one, two)); // true
    System.out.println(equals2D(three, four)); // true
}
/**
 * The two 2D arrays are equal if they contain the same rows
 * in the same order. Also, two array references are considered
 * equal if both are 'null'.
 */
public static boolean equals2D(Object[][] a1, Object[][] a2) {
    if (a1 == a2) return true;

    if (a1 == null || a2 == null || a1.length != a2.length)
        return false;

    for (int i = 0; i < a1.length; i++)
        if (!equals1D(a1[i], a2[i]))
            return false;

    return true;
}
/**
 * The two rows are equal if they contain the same elements
 * in the same order. Also, two row references are considered
 * equal if both are 'null'.
 */
public static boolean equals1D(Object[] a1, Object[] a2) {
    if (a1 == a2) return true;

    if (a1 == null || a2 == null || a1.length != a2.length)
        return false;

    for (int i = 0; i < a1.length; i++)
        if (!Objects.equals(a1[i], a2[i]))
            return false;

    return true;
}