I would like to know what is the best, fastest and easiest way to compare between 2-dimension arrays of integer. the length of arrays is the same. (one of the array's is temporary array)
我想知道在二维整数数组之间进行比较的最佳,最快和最简单的方法是什么。数组的长度是一样的。 (其中一个数组是临时数组)
2 个解决方案
#1
11
Edan wrote:
伊丹写道:
just need to see if the value is the same
只需要查看值是否相同
If you want to check that a[i][j]
equals b[i][j]
for all elements, just use Arrays.deepEquals(a, b)
.
如果你想检查所有元素的[i] [j]等于b [i] [j],只需使用Arrays.deepEquals(a,b)。
#2
4
Look at java.util.Arrays
; it has many array utilities that you should familiarize yourself with.
看看java.util.Arrays;它有许多你应该熟悉的数组实用程序。
import java.util.Arrays;
int[][] arr1;
int[][] arr2;
//...
if (Arrays.deepEquals(arr1, arr2)) //...
From the API:
来自API:
Returns
true
if the two specified arrays are deeply equal to one another. Unlike theequals(Object[],Object[])
method, this method is appropriate for use with nested arrays of arbitrary depth.如果两个指定的数组彼此非常相等,则返回true。与equals(Object [],Object [])方法不同,此方法适用于任意深度的嵌套数组。
Note that in Java, int[][]
is a subtype of Object[]
. Java doesn't really have true two dimensional arrays. It has array of arrays.
请注意,在Java中,int [] []是Object []的子类型。 Java并不真正具有真正的二维数组。它有数组数组。
The difference between equals
and deepEquals
for nested arrays is shown here (note that by default Java initializes int
arrays with zeroes as elements).
这里显示了嵌套数组的equals和deepEquals之间的区别(请注意,默认情况下,Java使用零作为元素初始化int数组)。
import java.util.Arrays;
//...
System.out.println((new int[1]).equals(new int[1]));
// prints "false"
System.out.println(Arrays.equals(
new int[1],
new int[1]
)); // prints "true"
// invoked equals(int[], int[]) overload
System.out.println(Arrays.equals(
new int[1][1],
new int[1][1]
)); // prints "false"
// invoked equals(Object[], Object[]) overload
System.out.println(Arrays.deepEquals(
new int[1][1],
new int[1][1]
)); // prints "true"
Related questions
- Java
Arrays.equals()
returnsfalse
for two dimensional arrays - Java Arrays.equals()为二维数组返回false
#1
11
Edan wrote:
伊丹写道:
just need to see if the value is the same
只需要查看值是否相同
If you want to check that a[i][j]
equals b[i][j]
for all elements, just use Arrays.deepEquals(a, b)
.
如果你想检查所有元素的[i] [j]等于b [i] [j],只需使用Arrays.deepEquals(a,b)。
#2
4
Look at java.util.Arrays
; it has many array utilities that you should familiarize yourself with.
看看java.util.Arrays;它有许多你应该熟悉的数组实用程序。
import java.util.Arrays;
int[][] arr1;
int[][] arr2;
//...
if (Arrays.deepEquals(arr1, arr2)) //...
From the API:
来自API:
Returns
true
if the two specified arrays are deeply equal to one another. Unlike theequals(Object[],Object[])
method, this method is appropriate for use with nested arrays of arbitrary depth.如果两个指定的数组彼此非常相等,则返回true。与equals(Object [],Object [])方法不同,此方法适用于任意深度的嵌套数组。
Note that in Java, int[][]
is a subtype of Object[]
. Java doesn't really have true two dimensional arrays. It has array of arrays.
请注意,在Java中,int [] []是Object []的子类型。 Java并不真正具有真正的二维数组。它有数组数组。
The difference between equals
and deepEquals
for nested arrays is shown here (note that by default Java initializes int
arrays with zeroes as elements).
这里显示了嵌套数组的equals和deepEquals之间的区别(请注意,默认情况下,Java使用零作为元素初始化int数组)。
import java.util.Arrays;
//...
System.out.println((new int[1]).equals(new int[1]));
// prints "false"
System.out.println(Arrays.equals(
new int[1],
new int[1]
)); // prints "true"
// invoked equals(int[], int[]) overload
System.out.println(Arrays.equals(
new int[1][1],
new int[1][1]
)); // prints "false"
// invoked equals(Object[], Object[]) overload
System.out.println(Arrays.deepEquals(
new int[1][1],
new int[1][1]
)); // prints "true"
Related questions
- Java
Arrays.equals()
returnsfalse
for two dimensional arrays - Java Arrays.equals()为二维数组返回false