简体   繁体   English

返回二维数组最大值的方法 Java

[英]Method that returns the maximum of a 2D-Array Java

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.我不擅长java中的Generics。

There are two issues in your code:您的代码中有两个问题:

  • The Comparable::compareTo method doesn't return boolean but int . Comparable::compareTo方法不返回booleanint Here is the description of the method:以下是该方法的说明:

    Compares its two arguments for order.比较其两个 arguments 的订单。 Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.返回负 integer、零或正 integer,因为第一个参数小于、等于或大于第二个。 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.在前面的描述中,符号sgn(表达式)表示数学符号function,它被定义为根据表达式的值是负数、零还是正数返回-1、0或1之一。

  • You cannot instantiate the generic object such as new E() .您不能实例化通用 object,例如new E() Check Instantiating generics type in java .在 java 中检查实例化 generics 类型

This is a way to go:这是 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, ie the arrays are not empty.唯一的条件是元素list[0][0]存在,即arrays 不为空。 Otherwise, you should modify the solution to use Optional<E> since the maximum is not always found (empty arrays).否则,您应该修改解决方案以使用Optional<E>因为并不总是找到最大值(空数组)。

As of Java 8, there is a simple way to handle such use cases using :从 Java 8 开始,有一种使用处理此类用例的简单方法:

Optional<E> optionalmaximum = Arrays.stream(list)                    // Stream<E[]>
                                    .flatMap(Arrays::stream)         // Stream<E>
                                    .max(Comparator.naturalOrder()); // Optional<E>

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM