Takes an array and counts how many of its elements equals with the parameter - Java Collection Framework

Java examples for Collection Framework:Array Equal

Description

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

Demo Code


//package com.java2s;

public class Main {
    /**//from   w  w  w.  j a va2s.c  o m
     * 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)) {
                counter++;
            }
        }
        return counter;
    }
}

Related Tutorials