集册 Java实例教程 检查数组中是否包含元素的泛型方法

检查数组中是否包含元素的泛型方法

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

381
检查数组中是否包含元素的泛型方法


//package com.nowjava;


public class Main {/**N o w  J a v a  . c o m**/


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

        final Class<?> componetType = array.getClass().getComponentType();

        boolean isPrimitive = false;

        if (null != componetType) {

            isPrimitive = componetType.isPrimitive();

        }

        for (T t : array) {

            if (t == value) {

                return true;

            } else if (false == isPrimitive && null != value

                    && value.equals(t)) {

         
展开阅读全文