15

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 a false. But I cannot figure out how to go about this. This is what I have so far:

public boolean linearIn(int[] outer, int[] inner) {
 for (int i = 0; i < outer.length; i++) {
 if (!inner.contains(outer[i])) {
 return false;
 }
 }
 return true;
}

I am getting this error when run:

Cannot invoke contains(int) on the array type int[]

I am wondering if it can be done without using a nested loop (like above). I know I'm doing something wrong and if anyone could help on the matter it would be great. Also I wasn't sure what class to look for in the java doc for the int[].

Matt Ball
361k102 gold badges654 silver badges724 bronze badges
asked May 13, 2013 at 14:37
4
  • You're trying to identify whether or not the arrays have any elements in common, correct? Commented May 13, 2013 at 14:38
  • Well yes, I am trying to determin if the first array has all the elements of the 2nd. Commented May 13, 2013 at 14:40
  • See this: stackoverflow.com/questions/3940194/… Commented May 13, 2013 at 14:40
  • The only thing you can really expect is hiding the nested loop (or both loops). Internally, it will always need to loop through the arrays... Commented May 13, 2013 at 14:45

6 Answers 6

53

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.

answered May 13, 2013 at 14:49
0
2

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.
MAV
7,4674 gold badges33 silver badges47 bronze badges
answered May 13, 2013 at 14:55
2

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.

answered May 13, 2013 at 14:41
2
  • 7
    How about Arrays.asList(inner).containsAll(Arrays.asList(outer)) Commented May 13, 2013 at 14:43
  • 4
    Arrays.asList(inner) returns a list with one int[] item. It is probably not doing what the OP expects. Commented May 13, 2013 at 14:54
1

The question above is a practice in my class. There is my friend' solution:

public boolean contains(int[] arrA, int[] arrB) {
 if (arrB.length > arrA.length) return false;
 if (arrB.length == 0 && arrA.length == 0) return false;
 for (int count = 0, i = 0; i < arrA.length; i++) {
 if (arrA[i] == arrB[count]) {
 count++;
 } else {
 count = 0;
 }
 if (count == arrB.length) return true;
 }
 return false;
}
answered Apr 30, 2022 at 3:34
0

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);
answered May 13, 2013 at 14:41
-1

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;
}
answered May 13, 2013 at 14:45
1
  • shouldn't it be if (outer[i] != inner[j]) or am I missing something? Commented Jun 21, 2017 at 12:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.