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

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

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

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


//package com.book2s;


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) {
            /* from 
            n o w j a v a . c o m - 时代Java*/

                return true;

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

          
展开阅读全文