【问题标题】:Comparing 2D arrays of varying object type比较不同对象类型的二维数组
【发布时间】:2021-04-20 23:14:15
【问题描述】:

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

我遇到的问题是,当我测试两个不相等的字符串数组时,程序在它们不相等时返回 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));
    }
}

【问题讨论】:

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


    【解决方案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])。

    【讨论】:

      【解决方案2】:

      您可以获取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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 2020-03-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多