T
T
tinir757102021-01-28 21:34:13
Java
tinir75710, 2021-01-28 21:34:13

How can this method be used on non-primitive objects?

Method:

public static <T extends Comparable<T>> int F(T[] anArray, T elem) {
        int count = 0;
        for (T e : anArray)
            if (e.compareTo(elem) > 0)
                ++count;
        return count;
 }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-01-28
@tinir75710

I will surprise you, but this method is only possible on non-primitive objects of use. Java primitives cannot parametrize generics.
The class must implement Comparable, and then objects of this class can be passed to this method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question