Checks to see if this array contains an object. - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

Checks to see if this array contains an object.

Demo Code


//package com.java2s;

public class Main {
    /**/* w w w.jav  a  2  s.c o  m*/
     * Checks to see if this array contains an object. This method uses the {@link Object#equals(Object)} function and the
     * {@link Object#hashCode()} function to compare
     * @param a The array to check
     * @param obj The object that should be inside the array
     * @param <T> The type of this array
     * @return True if the item is inside the array, false otherwise
     */
    public static <T> boolean contains(T[] a, T obj) {
        for (T item : a) {
            if (item.equals(obj) || item.hashCode() == obj.hashCode())
                return true;
        }

        return false;
    }
}

Related Tutorials