[英]How to check if all elements in one array exist in other array. However in the same order
I've been trying to solve the issue of searching through array1 to return a boolean if the elements of array1 can be found in array2.如果可以在 array2 中找到 array1 的元素,我一直在尝试解决搜索 array1 以返回 boolean 的问题。 However, it can only be true if array2 contains array1 elements in the same order .
但是,只有当 array2以相同的顺序包含 array1 元素时才为真。
For example, array1 = {3,4,5,6}
and array2 = {3, 4, 1, 2, 7, 5, 6}
would be true
since all elements are found in the same order.例如,
array1 = {3,4,5,6}
和array2 = {3, 4, 1, 2, 7, 5, 6}
将为true
,因为所有元素都以相同的顺序找到。
Here's my code so far:到目前为止,这是我的代码:
int size = Queue1.size();
for(int i = 0; i < size; i++) {
arr3[i] = Queue1.dequeue();
}
boolean test = Arrays.asList(arr2).containsAll(Arrays.asList(arr3));
System.out.println(test);
The variable test returns to be false
for the input of变量 test 对于输入返回为
false
arr3 = {3,4,5,6}
arr2 = {3,4,2,1,5,10,9,8,6,7}
This is a simple algorithm to resolve your problem, base on List<Integer>
contains
and indexOf
functions:这是一个解决问题的简单算法,基于
List<Integer>
contains
和indexOf
函数:
code (with comments)
:代码
(with comments)
:
public static void main(String[] args) {
Integer[] array1 = new Integer[]{3,4,5,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};
List<Integer> list1 = Arrays.asList(array1);
List<Integer> list2 = Arrays.asList(array2);
boolean check = false;
int i = 0;
for (Integer n : list1) {
if (i == 0) {
if (list2.contains(n)) {// first element check if it's on the second array
check = true;
}else check = false;
}else {
// check if the element in the array2 and its index is > of the previous element
if (list2.contains(n) && list2.indexOf(n) > list2.indexOf(list1.get(i-1))) {
check = true;
}else {
check = false;
break;
}
}
i++;
}
System.out.println(check);
}
result:结果:
input 1:
Integer[] array1 = new Integer[]{3,4,5,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};
output 1: true
input 2:
Integer[] array1 = new Integer[]{3,7,4,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};
output 2: false
You are using the right approach, there's a suttle thing that you have to pay attention when using Arrays.asList , look:您使用的是正确的方法,在使用Arrays.asList时需要注意一件小事,请看:
If you pass an array of int[] to asList() it will return a List<int[]> rather than a List of Integer .如果将int[]数组传递给asList() ,它将返回List<int[]>而不是Integer 的列表。
But if you pass an array of Integer[] to asList() it will return a List of Integer and you will be able to compare it.但是,如果您将一个Integer[]数组传递给asList() ,它将返回一个Integer 的列表,您将能够对其进行比较。
See if on code it is more clear:看看代码上是否更清楚:
public static void main(String[] args) {
Integer[] arr1 = {1, 2, 3, 4};
Integer[] arr2 = {1, 2, 3, 4, 5};
boolean wrappers = checkWrappers(arr2, arr1);
System.out.println(wrappers); // true
int[] err1 = {1, 2, 3, 4};
int[] err2 = {1, 2, 3, 4, 5};
boolean primitives = checkPrimitives(err2, err1);
System.out.println(primitives); // false
}
public static boolean checkWrappers(Integer[] outer, Integer[] inner) {
List<Integer> integers2 = Arrays.asList(outer);
List<Integer> integers1 = Arrays.asList(inner);
return integers2.containsAll(integers1);
}
public static boolean checkPrimitives(int[] outer, int[] inner) {
List<int[]> ints2 = Arrays.asList(outer);
List<int[]> ints1 = Arrays.asList(inner);
return ints2.containsAll(ints1); // compare object references not content
}
When passing primitives it compares the int[] object reference instead of comparing each element as you would want to.传递基元时,它会比较 int[] object引用,而不是像您希望的那样比较每个元素。
Two simple solutions.两个简单的解决方案。
The idea in both is to see, for each element x in the first list, whether it is in the second list.两者的想法都是,对于第一个列表中的每个元素 x,它是否在第二个列表中。 If it is, we focus on the remaining part of the list (subList).
如果是,我们关注列表的剩余部分(subList)。
The second method works, because indexOf returns -1 if the element was not found.第二种方法有效,因为如果未找到该元素,indexOf 将返回 -1。
private static boolean test(List<Integer> list1, List<Integer> list2) {
int i = 0;
for (int x : list1) {
List<Integer> tempList = list2.subList(i, list2.size());
if (tempList.contains(x)) {
i = list2.indexOf(x);
}
else return false;
}
return true;
}
private static boolean test2(List<Integer> list1, List<Integer> list2) {
int i = 0;
int j = 0;
int len = list2.size();
for (int x : list1) {
j = list2.subList(i, len).indexOf(x);
if (j < 0) return false;
i += j;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.