0

I had this snippet in my code to check if every value of an array was contained in another array (basically for filtering).

This is the snippet

public boolean linearIn(int[] subarray, int[] array) {

    for (int i = 0; i < subarray.length; i++) {
        if (!Arrays.asList(array).contains(subarray[i])) {
            Log.d(TAG, "Array doesn't contain " + subarray[i]);
            return false;
        }
    }
    return true;
}

This used to work. I remember because it worked in an app I am working on. What do I mean by it worked? Well actually it could understand when there were or there weren't matches, indeed a filter was applied.

Now it turns out that it just never works. With the Log it outputs that doesn't find 0 even when I know that array contains a 0.

But luckily Android Studio isn't silent about this. This is the warning that gives at the if-statement:

List<int[]>' may not contain objects of type 'Integer' more... (CTRL+F1)

At the time I was inspired by the 3rd answer in this question.

And I was satisfied since it worked on my code, and it's pretty easy to say this since now it couldn't even find one match.

Now I resolved using Integer types (a bit unconfortable since it requires a loop for casting if I must have an int[])

Is it possible that SDK changed in the updates in a way that broke this code snipped?

Community
  • 1
  • 1
BlackBox
  • 631
  • 1
  • 5
  • 19
  • 2
    Notice assylias' comment there. You might of thought it worked, but it definitely does not, and has not. – Sotirios Delimanolis Jul 01 '16 at 14:30
  • 1
    `Arrays.asList(array)` is of type `List`, not `List`. – Andy Turner Jul 01 '16 at 14:30
  • I ... know that? But how is it possible I **had** matches previous tests? (like 2 months ago before I updated SDK). Now it can't even have one. It seems strange I could not notice it before, since it used to have matches. Now it just **can't** have one... I surely would have noticed it. And by how I developed the app next, I assure I did work on actually found matches, or I could not have developed and tested some parts of the app which make use of filtered data. – BlackBox Jul 01 '16 at 14:35
  • 1
    The only way this can have previously worked is if `subarray.length == 0` when you tried it. This behaviour has not changed - it would only have worked if there was previously a method `asList(int[])` - which there wasn't. – Andy Turner Jul 01 '16 at 14:47
  • Arrays.binarySearch not suitable? – Memme Jul 01 '16 at 15:06
  • @Memme I have to check that ALL the elements in an array have to be in the other... You mean a n-uple binary search? That may have been interesting. Now that I think about it, my elements are sorted, but because they're happened to be. I mean, there would still no way to make an unsorted array just using the app (I suppose), but I don't want that in such case there would be unexpected behaviors, because I don't expect array to be always sorted (even if they are). A thing that would have helped is that it's open to int[] and int types... Why isn't `contains` overriden to accept primitive types? – BlackBox Jul 01 '16 at 15:19

1 Answers1

0
Arrays.asList(array) 

creates a

List<int[]>

not a

List<Integer>. 
Alex Pruss
  • 533
  • 5
  • 15
  • Yeah, but instead the function contains apparently casts to 'Integer'. I'm just asking if it has always worked like that. I'm sure it used to work – BlackBox Jul 01 '16 at 14:33
  • Arrays.asList() has not had its method signature changed in Java 8, no. The cast you're referring to is autoboxing, since an 'int' is a primitive, not a (Number) object. – Alex Pruss Jul 01 '16 at 14:35