Generic method to check if an array contains a value - Android java.lang

Android examples for java.lang:array

Description

Generic method to check if an array contains a value

Demo Code

public class Main {

  public static <T> boolean contains(final T v, final T... array) {
    if (v == null) {
      for (final T e : array)
        if (e == null)
          return true;
    } else {//from ww  w.  j a v a  2 s  .co  m
      for (final T e : array)
        if (e == v || v.equals(e))
          return true;
    }
    return false;
  }

  public static boolean contains(final int[] array, final int key) {
    for (final int i : array) {
      if (i == key) {
        return true;
      }
    }
    return false;
  }

}

Related Tutorials