如何在 indexOf 中使用 int[][] 中的列表?
How to use in indexOf with a List out of int[][]?
这是我创建的有效方法。我用了Arrays.deepEquals。它检查 int[][] 是否在 int[][] 之外的 ArrayList 中。感谢 Thomas 给出解决方案!
public boolean contains(int[][]matrix1, List<int[][]> matrice){
boolean contains = false;
for(int[][] m : matrice){
if(Arrays.deepEquals(m, matrix)){
contains = true;
index = matrice.indexOf(m);
}
}
return contains;
}
我有以下代码。我想从与矩阵具有相同值的矩阵中获取索引。我认为它不起作用,因为我正在检查引用而不是值。我只是不知道应该怎么做。
List<int[2][2]> matrice = new ArrayList<int[][]>();
int[][] matrix = new int[2][2]
public void testMethod(){
// here matrix gets a value
matrix = {{1,4}{3,2}};
//Here List matrice gets filled with different matrice (4x)
...
//add a copy of matrix to matrice
matrice.add(copy2dArray(matrix));
int index = matrice.indexOf(matrix);
System.out.println("matrix ->"Arrays.deepToString(matrix));
System.out.println("matrice[4] ->"Arrays.deepToString(matrice[4]));
System.out.println("index = "+index);
System.out.println(matrice.contains(matrix));
}
private int[][] copy2dArray(int[][] original){
int[][] copy = new int[original.length][];
for(int i = 0; i < original.length; i++){
copy[i] = Arrays.copyOf(original[i], original[i].length);
}
return copy;
}
输出:
matrix -> [[1,4],[3,2]]
matrice[4] -> [[1,4],[3,2]]
index = -1
false
输出应该是:
matrix -> [[1,4],[3,2]]
matrice[4] -> [[1,4],[3,2]]
index = 4
true
问题在于 ArrayList.indexOf()
(与大多数其他实现一样)遍历元素并对每个元素调用 equals()
直到匹配一个。然后返回它的索引,在你的例子中应该是 0(不是 4)。
但是,数组没有定义自己的 equals()
实现,因此使用 Object
中定义的默认实现,只有 returns 如果数组是 完全相同的实例(由于您复制了数组,它们不是)。
要解决此问题,您可以使用包含数组并适当实现 equals()
(和 hashCode()
)的包装器。 "wrapper" 可以称为 Matrix
并且可能还会导致更好的设计 ;)
示例:
class Matrix {
int[][] theCells;
public boolean equals(Object o) {
//compare the indivual arrays, e.g. by using `Arrays.deepEquals()`,
//which takes care of multidimensional arrays
}
}
List<Matrix> matrices = new ArrayList<>();
这是我创建的有效方法。我用了Arrays.deepEquals。它检查 int[][] 是否在 int[][] 之外的 ArrayList 中。感谢 Thomas 给出解决方案!
public boolean contains(int[][]matrix1, List<int[][]> matrice){
boolean contains = false;
for(int[][] m : matrice){
if(Arrays.deepEquals(m, matrix)){
contains = true;
index = matrice.indexOf(m);
}
}
return contains;
}
我有以下代码。我想从与矩阵具有相同值的矩阵中获取索引。我认为它不起作用,因为我正在检查引用而不是值。我只是不知道应该怎么做。
List<int[2][2]> matrice = new ArrayList<int[][]>();
int[][] matrix = new int[2][2]
public void testMethod(){
// here matrix gets a value
matrix = {{1,4}{3,2}};
//Here List matrice gets filled with different matrice (4x)
...
//add a copy of matrix to matrice
matrice.add(copy2dArray(matrix));
int index = matrice.indexOf(matrix);
System.out.println("matrix ->"Arrays.deepToString(matrix));
System.out.println("matrice[4] ->"Arrays.deepToString(matrice[4]));
System.out.println("index = "+index);
System.out.println(matrice.contains(matrix));
}
private int[][] copy2dArray(int[][] original){
int[][] copy = new int[original.length][];
for(int i = 0; i < original.length; i++){
copy[i] = Arrays.copyOf(original[i], original[i].length);
}
return copy;
}
输出:
matrix -> [[1,4],[3,2]]
matrice[4] -> [[1,4],[3,2]]
index = -1
false
输出应该是:
matrix -> [[1,4],[3,2]]
matrice[4] -> [[1,4],[3,2]]
index = 4
true
问题在于 ArrayList.indexOf()
(与大多数其他实现一样)遍历元素并对每个元素调用 equals()
直到匹配一个。然后返回它的索引,在你的例子中应该是 0(不是 4)。
但是,数组没有定义自己的 equals()
实现,因此使用 Object
中定义的默认实现,只有 returns 如果数组是 完全相同的实例(由于您复制了数组,它们不是)。
要解决此问题,您可以使用包含数组并适当实现 equals()
(和 hashCode()
)的包装器。 "wrapper" 可以称为 Matrix
并且可能还会导致更好的设计 ;)
示例:
class Matrix {
int[][] theCells;
public boolean equals(Object o) {
//compare the indivual arrays, e.g. by using `Arrays.deepEquals()`,
//which takes care of multidimensional arrays
}
}
List<Matrix> matrices = new ArrayList<>();