Check if an array contains a value - Android java.lang

Android examples for java.lang:array

Description

Check if an array contains a value

Demo Code


//package com.java2s;

import java.util.Collection;

public class Main {
    /**//w ww.  jav  a 2s  .  com
     * @return true if the array contains the given value, false otherwise
     */
    public static <T> boolean contains(T[] array, T value) {
        if (isEmpty(array)) {
            return false;
        }

        for (T item : array) {
            if (item != null && item.equals(value)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Is this list empty. Checks for null as well as size.
     *
     * @return true if empty, false if not
     */
    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Is this list empty. Checks for null as well as size.
     *
     * @return true if empty, false if not
     */
    public static boolean isEmpty(Object[] collection) {
        return collection == null || collection.length == 0;
    }
}

Related Tutorials