简体   繁体   English

比较2D阵列

[英]Comparing 2D arrays

Im trying to compare two 2D arrays by using this. 我试图通过使用此比较两个2D数组。 But I keep getting " Arrays are not the same. " even when they are 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.");

I can't do it like this either. 我也不能这样做。 boolean check1 = Arrays.equals(a2[i][j], a3[i][j]); 布尔值check1 = Arrays.equals(a2 [i] [j],a3 [i] [j]);

The first one does not work because a two-D int array is really an array of arrays (that is, an array of objects). 第一个无效,因为二维int数组实际上是一个数组数组(即对象数组)。 The Arrays.equals() method for an array of objects uses equals() to test whether corresponding elements are equal. 对象数组的Arrays.equals()方法使用equals()测试相应的元素是否相等。 Unfortunately for your code, equals() for arrays is the default Object implementation: they are equal() only if they are the identical object. 不幸的是,对于您的代码,数组的equals()是默认的Object实现:仅当它们是相同的对象时,它们才equal() In your case, they are not. 在您的情况下,它们不是。

In the second case, when you code Arrays.equals and pass two int values, the compiler can't match it to any signature of the Arrays class. 在第二种情况下,当您对Arrays.equals编码并传递两个int值时,编译器无法将其与Arrays类的任何签名匹配。

One way to check equality is to use deepEquals: 一种检查相等性的方法是使用deepEquals:

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

Another way is to iterate the outer array explicitly: 另一种方法是显式地迭代外部数组:

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

is definitely a possibility. 绝对有可能。 The implementation of that uses Object[] , which may be appealing to you because it checks the types of the arrays you pass on-the-fly. 该实现使用Object[] ,这可能对您有吸引力,因为它会检查您即时传递的数组的类型。

But if you want stronger typing and a little less overhead, you can write your own as follows. 但是,如果您想要更强的键入并减少一些开销,则可以如下编写自己的代码。

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;
    }
}

The first equals on 2D arrays calls Arrays.equals() . 2D数组上的第一个equals调用Arrays.equals() The 3D version similarly calls the 2D one. 3D版本类似地称为2D版本。

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM