集册 Java实例教程 获取一个数组并计算其元素中有多少与参数相等

获取一个数组并计算其元素中有多少与参数相等

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

459
获取一个数组,并计算其元素中有多少与该参数相等
/* 来自 N o w J a v a . c o m*/


//package com.nowjava;


public class Main {

    /**

     * Takes an array and counts how many of its elements equals with the

     * parameter

     *

     * @param <T> Type

     * @param typeClass the class of the type of the array

     * @param array the array you want to operate on

     * @param element the element you want to check

     * @return how many times the element exists in the array

     */

    public static <T> int contains(Class<T> typeClass, T[] array, T element) {

        int counter = 0;

        for (T t : array) {

            if (t.equals(element)) {

  
展开阅读全文