1
public static <E extends Comparable<E>> E getMaxElement(E[][] list){
    E theMaximumElement = new E();
    for (int i = 0; i <list.length ; i++) {
        for (int j = 0; j <list[i].length ; j++) {

            if(theMaximumElement.compareTo(list[i][j]))
                theMaximumElement = list[i][j];
        }
    }
    return theMaximumElement;
}

How can ı write this code well? Is it true?

I want to find the maximum element. I am not good at Generics in java.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Yusuf T.
  • 63
  • 4
  • 2
    You can't create instances of generic types (that also wouldn't make sense in this context). Use the first element of `list` instead (`list[0][0]`). Also add a `> 0` to your if-statement. – cegredev Apr 19 '20 at 16:08

2 Answers2

2

There are two issues in your code:

  • The Comparable::compareTo method doesn't return boolean but int. Here is the description of the method:

    Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.

  • You cannot instantiate the generic object such as new E(). Check Instantiating generics type in java.

This is a way to go:

public static <E extends Comparable<E>> E getMaxElement(E[][] list) {

    E theMaximumElement = list[0][0];                // initial value (the first one)
    for (final E[] array : list) {
        for (final E e : array) {
            if (theMaximumElement.compareTo(e) < 0)  // comparison is against 0
                theMaximumElement = e;               // setting the new maximum value
        }
    }
    return theMaximumElement;                        // returning it
}

The only condition is that an element list[0][0] exists, i.e. the arrays are not empty. Otherwise, you should modify the solution to use Optional<E> since the maximum is not always found (empty arrays).

As of Java 8, there is a simple way to handle such use cases using :

Optional<E> optionalmaximum = Arrays.stream(list)                    // Stream<E[]>
                                    .flatMap(Arrays::stream)         // Stream<E>
                                    .max(Comparator.naturalOrder()); // Optional<E>
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
2

This is my version:

public static <E extends Comparable<E>> E getMaxElement(E[][] list) {
    if (list.length <= 0)
        return null;

    E theMaximumElement = list[0][0];

    for (int i = 0; i < list.length; i++)
        for (int j = 0; j < list[i].length; j++)
            if (list[i][j].compareTo(theMaximumElement) > 0)
                theMaximumElement = list[i][j];

    return theMaximumElement;
}
cegredev
  • 1,485
  • 2
  • 11
  • 25
  • is it True? You have used for iteratorr such as me but nicolas haven't. – Yusuf T. Apr 19 '20 at 16:42
  • 2
    @YusufT. I used an enhanced loop, not a "for iterator". And as long as you are a new member, if one of our answers resolved your issue, we'd be happy to suggest you upvote (the way saying "thanks, it helped me", this action is optional) and marking one of the answers as accepted to mark this question as resolved so others will know. – Nikolas Charalambidis Apr 19 '20 at 16:50
  • 2
    Nikolas - Great way of conveying the message! While the answer by @Schred is easy for the beginners to understand, your answer has options which all Java programmers should know as they mature. Schred is a great learner and has above all, he is a person with a big heart. Thumbs up to both the answer! – Arvind Kumar Avinash Apr 21 '20 at 08:12
  • 1
    Cannot agree more. – Nikolas Charalambidis Apr 21 '20 at 08:34