0
class MaximumTest {
    // determines the largest of three Comparable objects
    public static <T extends Comparable<T>> int CountDuplicates(T[] anArray, T elem) {
        int count = 0;
        for (T e: anArray)
            if (e.compareTo(elem) == 0)
            ++count;
        return count;
    }
    public static void main(String args[]) {
        double[] D_arr = {
            1.2, 3.4, 0.0, 4.5, 0.0
        };
        int[] I_arr = {
            0, 0, 0, 0, 1, 3, 5
        };
        System.out.println("No of zeros in Double Array = " + CountDuplicates(D_arr, 0.0));
        System.out.println("No of zeros in Double Array = " + CountDuplicates(I_arr, 0));
    }
}

What is the mistake in this code??

2
  • 2
    Have you tried running it? It shows you an error message which tells you what the problem might be. Commented May 15, 2018 at 13:31
  • 2
    how hard is to put that into an IDE? Commented May 15, 2018 at 13:33

1 Answer 1

4

Your arrays should be of reference type:

Double[] D_arr = {1.2, 3.4, 0.0, 4.5, 0.0};
Integer[] I_arr = {0,0,0,0,1,3,5};

A primitive type cannot be used as a generic type.

After this change, your code will pass compilation and produce the output:

No of zeros in Double Array = 2
No of zeros in Double Array = 4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.