我正在尝试检查数组是否位于 LinkedList 甚至 Stack 结构中,如下所示:
LinkedList<int[]> list = new LinkedList();
int foo = new int[]{1,2,3};
int bar = new int[]{1,2,3};
list.addLast(bar);
if(list.contains(foo)) {
System.out.print("I found foo!");
} else {
System.out.print("I didn't find foo.");
}
这将返回“我没有找到 foo”。
当我尝试 if (Stack.search(foo) != -1)
时,结果相同,如果在 Stack
中找到 foo
就会成功。
如何在 LinkedList 或其他 Vector/List 类型结构中搜索特定数组?
最佳答案
目前按照上面的代码。您尚未将 foo 添加到链接列表中。 并且您还没有正确初始化 int 数组。
LinkedList<int[]> list = new LinkedList();
int[] foo = new int[]{1,2,3};
int[] bar = new int[]{1,2,3};
list.addLast(foo);
list.addLast(bar);
if(list.contains(foo)) {
System.out.print("I found foo!");
} else {
System.out.print("I didn't find foo.");
}
关于java - 在 Java 中查找列表中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29618872/