集册 Java实例教程 检查此数组是否包含对象。

检查此数组是否包含对象。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

398
检查此数组是否包含对象。
//来自 N o  w  J a v a . c o m - 时  代  Java


//package com.nowjava;


public class Main {

    /**

     * 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.
展开阅读全文