Android Array Contains contains(T[] array, T value)

Here you can find the source of contains(T[] array, T value)

Description

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

License

Apache License

Parameter

Parameter Description
array the array to check in
value the value to check for

Return

true if the value is present in the array

Declaration

public static <T> boolean contains(T[] array, T value) 

Method Source Code

//package com.java2s;
/*// w  w w  .  ja  v  a  2 s. co  m
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * 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;
    }

    /**
     * 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

  1. contains(Object[] array, Object objectToFind)
  2. contains(T[] array, T e)
  3. contains(T[] array, T value)
  4. contains(T[] array, T value)
  5. contains(T[] array, T value)
  6. contains(boolean[] array, boolean valueToFind)
  7. contains(byte[] array, byte valueToFind)
  8. contains(char[] array, char valueToFind)