Finding if an array contains all elements in another array

后端 未结 5 1152
旧时难觅i
旧时难觅i 2020-11-29 10:53

I am trying to loop through 2 arrays, the outer array is longer then the other. It will loop through the first and if the 2nd array does not contain that int it will return

相关标签:
5条回答
  • 2020-11-29 11:00

    You could check that the larger of the arrays outer contains every element in the smaller one, i.e. inner:

    public static boolean linearIn(Integer[] outer, Integer[] inner) {
    
       return Arrays.asList(outer).containsAll(Arrays.asList(inner));
    }
    

    Note: Integer types are required for this approach to work. If primitives are used, then Arrays.asList will return a List containing a single element of type int[]. In that case, invoking containsAll will not check the actual content of the arrays but rather compare the primitive int array Object references.

    0 讨论(0)
  • 2020-11-29 11:03

    If you would like to use contains then you need an ArrayList. See: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#contains(java.lang.Object)

    Otherwise, you need two loops.

    There is a workaround like this:

    public boolean linearIn(int[] outer, int[] inner) {
        List<Integer> innerAsList = arrayToList(inner);
        for (int i = 0; i < outer.length; i++) {
          if (!innerAsList.contains(outer[i])) {
             return false;
          }
       }
       return true;
    }  
    
    private List<Integer> arrayToList(int[] arr) {
        List<Integer> result= new ArrayList<Integer>(arr.length);
        for (int i : arr) {
            result.add(i);
        }
        return result;
    }
    

    But don't think that looping is not happening, just because you don't see it. If you check the implementation of the ArrayList you would see that there is a for loop: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java#ArrayList.indexOf(java.lang.Object) So you are not gaining any performance. You know your model best, and you might be able to write more optimized code.

    0 讨论(0)
  • 2020-11-29 11:05

    int[] is a primitive array. Meaning it does not have any special methods attached to it. You would have to manually write your own contains method that you can pass the array and the value to.

    Alternatively you could use an array wrapper class such as ArrayList which does have a .contains method.

    ArrayList<Integer> inner = new ArrayList<Integer>();
    boolean containsOne = inner.contains(1);
    
    0 讨论(0)
  • 2020-11-29 11:07

    contain method is reserved for ArrayList Try this:

    public boolean linearIn(int[] outer, int[] inner) {
            for (int i = 0; i < outer.length; i++) {
                for (int j = 0; j < inner.length; j++) {
                    if (outer[i] == inner[j])
                        return false;
                }
            }
            return true;
    }
    
    0 讨论(0)
  • 2020-11-29 11:23

    You have two options using java.util.Arrays if you don't want to implement it yourself:

    • Arrays.toList(array).contains(x) which does exactly you are doing right now. It is the best thing to do if your array is not guaranteed to be sorted.
    • Arrays.binarySearch(x,array) provided if your array is sorted. It returns the index of the value you are search for, or a negative value. It will be much, much faster than regular looping.
    0 讨论(0)
提交回复
热议问题