Y
Y
Yaten2022-02-14 21:05:50
Java
Yaten, 2022-02-14 21:05:50

How to display the number of array elements greater than a given number?

the whole essence of the task, here:
Create class MyTestMethod with generic static method calcNum (with two parameters: an array T[] and variable maxElem of type T) that counts the number of elements in an array T[] that are greater than a specified element maxElem.

That is, how to search for the maximum number in the code, I understand:

double max = arra[0];
        for (int i = 0; i < arra.length; i++) {
            if (arra[i] > max)
             max = arra[i];
        }
        System.out.println("Max is " + max);


and the fact that using length you can determine the length of the array, this is understandable, it’s simple, but here’s how to use the knowledge of the maximum number to search through the array, displaying all the numbers after, I don’t understand, when you try to sketch out the code, you get something crazy , type:

public static <T> int calcNum(T[] array, T maxElem) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i; j < array.length; j++) {
                System.out.println((array[i, j]);
            }
        }

Thank you for your attention

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-02-14
@xez

Here's what Google translate says about it:


Create a class MyTestMethod with a generic static method calcNum (with two parameters: an array T[] and a variable maxElem of type T) that counts the number of elements in the array T[] greater than the specified maxElem.

Everything becomes clear immediately:
static <T extends Comparable<T>> int calcNum(T[] array, T maxElem) {
   int count = 0;
   // Тривиальная реализация подсчета количества элементов, больших чем maxElem.
   return count;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question