简体   繁体   English

如何检查一个数组中的所有元素是否存在于另一个数组中。 但是以相同的顺序

[英]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> containsindexOf函数:

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.

相关问题 如何检查所有数组的元素是否相同 - How to check if all of an array's elements are the same 包含相同初始数组副本的对象。 一个数组中的值更改会导致所有其他数组中的值更改。 我该如何预防? - Objects that contain copies of the same initial array. Value changed in one array leads to values changed in all other arrays. How can I prevent this? 如何检查除一个以外的所有数组值是否都相同? - How to check if all array values but one are the same? Java如何将数组的元素与同一数组的所有其他元素进行比较 - Java how to compare an element of an array with all the other elements of the same array 数组元素如何检查一个元素是否仅比另一个大1 - array elements how to check if one element is only 1 bigger than the other 如何检查数组元素是否相同 - How to check if array elements are the same 如何检查数组中的元素是否在另一个数组中重复。 所有这些数组都在多维数组中? - How to check if an element from an array is being repeated in another array. All these arrays are inside of a Multidimensional array? 如果行和列中的所有元素都相同,如何检查二维数组? - How to check in 2d array if all elements in row and column are the same? 在阵列上同步。 它是否在所有元素或数组对象上同步? - Synchronizing on an array. Does it synchronize on all the elements or on the array object? 如何比较两个对象数组以检查一个数组元素与其他数组的所有值? - How to compare two array of object to check one array element with all values of other array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM