比较2D数组

4

我正在尝试使用这个方法比较两个二维数组。但是,即使它们相同,我仍然会得到"Arrays are not the same."的结果。

     int i;
     int j = 0;
     int k;
     int l;

List<Integer> list = new ArrayList<Integer>();
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> zero = new ArrayList<Integer>();

 for ( i = 1; i <= 16; i++) {
    list.add(i);


}

//System.out.println(list); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Collections.shuffle(list.subList(1, 15));
System.out.println(list);

Collections.replaceAll(list, 16, 0);
   System.out.println(list);


// System.out.println(list); //[11, 5, 10, 9, 7, 0, 6, 1, 3, 14, 2, 4, 15, 13, 12, 8]

int[][] a2 = new int[4][4];
int [][] a3 = new int[4][4];
for ( i = 0; i < 4; i++) {
    for ( j = 0;  j< 4; j++) {


        a2[i][j] = list.get(i*4 + j);
        a3[i][j] = list.get(i*4 + j);
    }

}
for (int[] row : a2) {
System.out.print("[");
for (   int a : row)
    System.out.printf("%4d", a);
System.out.println("]");
}
for (int[] row : a3) {
System.out.print("[");
for (   int a : row)
    System.out.printf("%4d", a);
System.out.println("]");
}
 boolean check1 = Arrays.equals(a2, a3);
if(check1 == false)
System.out.println("Arrays are not same.");
else
System.out.println("Both Arrays are same.");

我也不能这样做。 布尔值 check1 = Arrays.equals(a2[i][j], a3[i][j]);


2
那是因为你不能这样比较二维数组。 - Brian Roach
逐行检查一维数组。 - Louis Wasserman
1
可能是Java中比较二维数组的重复问题的重复问题。 - Brian Roach
1
抱歉,我花了一点时间为您搜索。 - Brian Roach
好的。但是我无法执行这个操作,Arrays.equals(a2[i][j], a3[i][j])。 - MosesA
1
不要费力地逐个打印数组,可以使用 System.out.println(Arrays.toString(a));,虽然没有每个元素的格式,但会打印完整精度。 - Mark Jeronimus
2个回答

15
第一个方法不起作用,因为二维 int 数组实际上是一个数组的数组(也就是对象数组)。对于对象数组,Arrays.equals() 方法使用 equals() 来测试相应元素是否相等。不幸的是,对于数组来说,equals() 是默认的 Object 实现:只有当它们是同一个对象时,它们才会相等。在您的情况下,它们不是同一个对象。
在第二种情况下,当你编写 Arrays.equals 并传递两个 int 值时,编译器无法将其匹配到 Arrays 类的任何签名。
检查相等性的一种方法是使用 deepEquals
boolean check1 = Arrays.deepEquals(a2, a3);

另外一种方法是明确迭代外部数组:

boolean check1 = true;
for (int i = 0; check1 && i < a2.length; ++i) {
    check1 = Arrays.equals(a2[i], a3[i]);
}

3
boolean check1 = Arrays.deepEquals(a2, a3);

这肯定是一种可能性。其实现使用的是Object[],因为它可以在运行时检查您传递的数组类型,所以您可能会感兴趣。

但是如果您想要更强的类型和更少的开销,您可以按照以下方式编写自己的实现。

import java.util.Arrays;

/**
 * Operations on multi-dimensional arrays.
 * 
 * @author stephen harrison
 */
public class ArrayUtils {
    private ArrayUtils() {
        // Static methods only
    }

    public static <T> boolean equals(final T[][] a, final T[][] b) {
        if (a == b) {
            return true;
        }

        if (a == null || b == null) {
            return false;
        }

        if (a.length != b.length) {
            return false;
        }

        for (int i = 0; i < a.length; ++i) {
            if (!Arrays.equals(a[i], b[i])) {
                return false;
            }
        }

        return true;
    }

    public static <T> boolean equals(final T[][][] a, final T[][][] b) {
        if (a == b) {
            return true;
        }

        if (a == null || b == null) {
            return false;
        }

        if (a.length != b.length) {
            return false;
        }

        for (int i = 0; i < a.length; ++i) {
            if (!equals(a[i], b[i])) {
                return false;
            }
        }

        return true;
    }
}

对于二维数组,第一个equals调用Arrays.equals()。三维数组的版本同样调用二维数组的版本。

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接