34

I was wondering if it was better to have a method for this and pass the Array to that method or to write it out every time I want to check if a number is in the array.

For example:

public static boolean inArray(int[] array, int check) {

    for (int i = 0; i < array.length; i++) {
        if (array[i] == check) 
            return true;
    }

    return false;
}

Thanks for the help in advance!

deHaar
  • 17,687
  • 10
  • 38
  • 51
Eli
  • 353
  • 1
  • 3
  • 4

5 Answers5

53

Since atleast Java 1.5.0 (Java 5) the code can be cleaned up a bit. Arrays and anything that implements Iterator (e.g. Collections) can be looped as such:

public static boolean inArray(int[] array, int check) {
   for (int o : array){
      if (o == check) {
         return true;
      }
   }
   return false;
}

In Java 8 you can also do something like:

// import java.util.stream.IntStream;

public static boolean inArray(int[] array, int check) {
   return IntStream.of(array).anyMatch(val -> val == check);
}

Although converting to a stream for this is probably overkill.

msanjay
  • 2,255
  • 2
  • 19
  • 19
Philip Whitehouse
  • 4,293
  • 3
  • 23
  • 36
  • 3
    The java-8 version is incorrect, Arrays.asList(array) will yield a List with a single element and that is the array which is not the intention. Also a list doesn’t have an anyMatch method. Look into Arrays.stream or IntStream.of – Ousmane D. Apr 26 '18 at 07:37
  • Thanks @Aominè - fixed. – Philip Whitehouse Jul 04 '18 at 14:26
12

You should definitely encapsulate this logic into a method.

There is no benefit to repeating identical code multiple times.

Also, if you place the logic in a method and it changes, you only need to modify your code in one place.

Whether or not you want to use a 3rd party library is an entirely different decision.

jahroy
  • 22,322
  • 9
  • 59
  • 108
8

If you are using an array (and purely an array), the lookup of "contains" is O(N), because worst case, you must iterate the entire array. Now if the array is sorted you can use a binary search, which reduces the search time to log(N) with the overhead of the sort.

If this is something that is invoked repeatedly, place it in a function:

private boolean inArray(int[] array, int value)
{  
     for (int i = 0; i < array.length; i++)
     {
        if (array[i] == value) 
        {
            return true;
        }
     }
    return false;  
}  
Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • 2
    How is that different that the function in the original post? Also, as a personal request, braces are a great thing for people learning the language. – JustinKSU Jan 23 '13 at 21:36
  • 1
    @JustinKSU +1 for suggesting to _always_ use braces for if statements and for loops. And yes, the above code is identical to the OPs code. – jahroy Jan 23 '13 at 21:37
  • 1
    @JustinKSU just copy/pasted the OP's post (which is why there were no braces). Added the function to be the same as there was no change needed and it served to answer OP's two part question. – Woot4Moo Jan 23 '13 at 21:38
7

You can import the lib org.apache.commons.lang.ArrayUtils

There is a static method where you can pass in an int array and a value to check for.

contains(int[] array, int valueToFind) Checks if the value is in the given array.

ArrayUtils.contains(intArray, valueToFind);

ArrayUtils API

anataliocs
  • 10,427
  • 6
  • 56
  • 72
0

Using java 8 Stream API could simplify your job.

public static boolean inArray(int[] array, int check) {
    return Stream.of(array).anyMatch(i -> i == check);
}

It's just you have the overhead of creating a new Stream from Array, but this gives exposure to use other Stream API. In your case you may not want to create new method for one-line operation, unless you wish to use this as utility. Hope this helps!

Nisarg Patil
  • 1,509
  • 1
  • 16
  • 27