1

I am trying to see if an element in my array is contained in one of my other arrays. I don't want to use a nested for loop for this and I am curious as to why my .contains does not work.

Currently I have two arrays

double [] s2 = new double [4];
double [] match = new double [s2.length];

and I am trying to test if s2 contains any of the values in my match array

for (j = 0; j < s2.length; j++) 
        {
            if(Arrays.asList(s2).contains(match[j])){
                return true;
            }
       }

Does this have something to do with the double datatype I am using? If so is there a way I can still use .contains?

  • http://stackoverflow.com/a/30251235/1553851 – shmosel Nov 30 '16 at 03:57
  • Any specific reason you don't want to use a nested loop? – shmosel Nov 30 '16 at 03:58
  • You *are* using a nested loop, you're just using indirectly through `contains` (and you're creating a new list wrapper for `s2` on each pass). – chrylis -cautiouslyoptimistic- Nov 30 '16 at 04:22
  • Interesting I did not now know that @chrylis, so would that give me a running time O(n^2) ? Because it would now be a nested loop running n amount of times. – user2723295 Nov 30 '16 at 04:32
  • You're wanting to check whether *any* element in `s2` is in `match`? There are some other alternatives with better big-O, such as sorting one first, but the constant will usually come down in favor of just looping twice. – chrylis -cautiouslyoptimistic- Nov 30 '16 at 05:38
  • [Implementation code here](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java#ArrayList.contains%28java.lang.Object%29), though simple reasoning will demonstrate that the best big-O of finding an element in an unsorted list is O(n), expected success time is n/2, and failure time is always n. – chrylis -cautiouslyoptimistic- Nov 30 '16 at 05:39

2 Answers2

2

Addressing the specific problem: Arrays.asList does not create a List of your elements but a List of only one value: your double[] of elements (at index 0). You should iterate through the list yourself for a second for loop instead of creating a different object.


It can be done with Stream operations () instead of using loops, although the difference should be minimal.

DoubleStream.of(s2)      // Pipe the source array
    .distinct()          // Remove duplicate elements
                         // as they only need to be matched against once
    .anyMatch(value ->   // Consider if any value matches this condition
        IntStream.of(match)  // It equals any value in "match"
                 .distinct() // after removing duplicate elements
                 .anyMatch(matchValue -> matchValue == value));

For additional information about Streams - see these Oracle articles: Processing Data with Java SE 8 Streams Part 1, Part 2

Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
Unihedron
  • 10,902
  • 13
  • 62
  • 72
1

The problem is Arrays.asList(s2) does not return a List<Double> object as you would expect.

It returns a List<double[]> object. So the elements inside that array will be of type double[].

Generics doesn't work with primitive types well.

The solution is to convert the double[] into a Double[] somewhere.

Thihara
  • 7,031
  • 2
  • 29
  • 56