- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我创建的有效方法。我使用了Arrays.deepEquals。它检查 int[][] 是否位于 int[][] 之外的 ArrayList 内。谢谢托马斯提供解决方案!
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
中定义的默认实现,仅当数组是完全相同的实例(由于您复制了数组,所以它们不是)。
要解决此问题,您可以使用包含数组并适当实现 equals()
(和 hashCode()
)的包装器。该“包装器”可以称为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<>();
关于java - 如何在indexOf中使用int[][]之外的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896240/
我想检索字符串列表中的行号(从文件加载)。Indexof 似乎完全匹配。有没有办法检索带有通配符版本的 Indexof 的行?类似于 SL.Indexof('?sometext')? 谢谢! 最佳答案
我已经解析了一些 JSON,但我想获取从 JSON 获得的链接,在链接中找到一个使其不同的标识符(例如 www.foo.com/IDENTIFIER/home),并拥有该标识符作为一个字符串插入到另一
我正在浏览 jQuery 的源代码并碰到这个: return indexOf.call( array, elem ); - Line 683 我想知道这背后的逻辑是什么,为什么不这样做: return
我正在从一个带有空格后的非组合变音符号的字符串中创建一个子字符串。这样做时,我使用 .Contains() 检查字符串。然后执行子串。当我使用空格时 char内部 .IndexOf() ,程序按预期执
我正在使用 Jayway JsonPath 库版本 2.4.0。在 jsonPath 中使用 indexOf 函数,例如。 $.values[?(@.num.indexOf('101') != -1)
由于某种原因,GWT 模拟(客户端)jdk 方法 indexOf() 无法按预期工作。 示例:. 我有一个包含 3 个 NaN 对象的列表: List doubleList = new ArrayLi
Resharper 推荐我使用: int notesFirstSpaceIndex = notes.IndexOf(" ", StringComparison.Ordinal); 代替: int no
我想 String.indexOf(char)比String.indexOf(String)使用单个字符和单个字符串时(例如,'x' & "x") 为了确保我的猜测,我编写了如下简单的测试代码。 pu
我在 IE 8 中的数组顶部使用了 indexOf 方法,但它给出了错误(因为不支持它)。我选择使用 underscore.js 库。我用了_.indexOf(array, value, [isSor
这个问题已经有答案了: No overload for method, takes 0 arguments? (1 个回答) 已关闭 7 年前。 我似乎无法弄清楚为什么我总是收到此错误。 No ove
在 Firefox 和 Opera 上使用 Javascript 调用 indexOf 时出错。在 IE 中工作正常。 错误信息如下: 行动 function anonymous(Grid, Row,
在第一次渲染Use Effect时遇到一个特定的错误,我不确定如何解决。我认为这与该功能的异步性有关,但不确定。项目是一个Reactjs,Firebase,FiRestore Todo应用程序,根据用
在第一次渲染Use Effect时遇到一个特定的错误,我不确定如何解决。我认为这与该功能的异步性有关,但不确定。项目是一个Reactjs,Firebase,FiRestore Todo应用程序,根据用
我需要创建一个对字符串进行排序的函数。字符串中的每个单词都将包含一个数字。数字可以是 1 到 9(不能是 0)。 例如输入:“is2 Thi1s T4est 3a”,函数应返回“Thi1s is2 3
有没有一种方法可以在 Java 中使用 indexOf 在单个解析中查找给定文本中多个字符串的位置? 例如,我想在一次文本解析中为“你”和“ session ”做一个 indexOf“你今天能参加 s
我正在使用以下代码来搜索字符串中的值。 if (myValue.indexOf("Call") > -1) { //dosomething } 我该怎么做和或? (myValue.indexOf("
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
This question already has an answer here: Reference - What does this regex mean? (1 个回答) 1年前关闭。 我必须在
我把它写进了一个 REPL: case class Thingy(s: String) val things = List(Thingy("x"), Thingy("y")) things.index
在带有此查询的 JMESPath 中: people[].{"index":@.index,"name":name, "state":state.name} 在此示例数据上: { "people"
我是一名优秀的程序员,十分优秀!