Checks that value is present as at least one of the elements of the array. - Android java.lang

Android examples for java.lang:array

Description

Checks that value is present as at least one of the elements of the array.

Demo Code

public class Main {

  /**//from   w  w w  . j  a v a 2s .  c  o m
   * Checks that value is present as at least one of the elements of the array.
   * 
   * @param array
   *          the array to check in
   * @param value
   *          the value to check for
   * @return true if the value is present in the array
   */
  public static <T> boolean contains(T[] array, T value) {
    for (T element : array) {
      if (element == null) {
        if (value == null)
          return true;
      } else {
        if (value != null && element.equals(value))
          return true;
      }
    }
    return false;
  }

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

  /**
   * Checks if the beginnings of two byte arrays are equal.
   *
   * @param array1
   *          the first byte array
   * @param array2
   *          the second byte array
   * @param length
   *          the number of bytes to check
   * @return true if they're equal, false otherwise
   */
  public static boolean equals(byte[] array1, byte[] array2, int length) {
    if (array1 == array2) {
      return true;
    }
    if (array1 == null || array2 == null || array1.length < length || array2.length < length) {
      return false;
    }
    for (int i = 0; i < length; i++) {
      if (array1[i] != array2[i]) {
        return false;
      }
    }
    return true;
  }

}

Related Tutorials