Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在java中使用泛型来查找数组的最大值,但它只接受双数据类型,而不接受其他数据类型 @覆盖 公共双最大值(){ Double total=(Double)super.array[0]; for(T:super.array){ 如果(t==null){ 继续; } else if(t实例数){ 数字n=(数字)t; 如果(总计_Java_Generics - Fatal编程技术网

我在java中使用泛型来查找数组的最大值,但它只接受双数据类型,而不接受其他数据类型 @覆盖 公共双最大值(){ Double total=(Double)super.array[0]; for(T:super.array){ 如果(t==null){ 继续; } else if(t实例数){ 数字n=(数字)t; 如果(总计

我在java中使用泛型来查找数组的最大值,但它只接受双数据类型,而不接受其他数据类型 @覆盖 公共双最大值(){ Double total=(Double)super.array[0]; for(T:super.array){ 如果(t==null){ 继续; } else if(t实例数){ 数字n=(数字)t; 如果(总计,java,generics,Java,Generics,我在java中使用泛型查找数组的最大值,但它只接受双精度数据类型,而不接受其他数据类型请帮助我知道问题是(双精度)那么我如何解决这个问题呢?您的循环体已经正确地检查了每个元素的类型。因此,使用该逻辑初始化您的总计,首先将总计设置为空: @Override public Double max() { Double total = (Double)super.array[0]; for(T t : super.array) { if (t == null) {

我在java中使用泛型查找数组的最大值,但它只接受双精度数据类型,而不接受其他数据类型请帮助我知道问题是(双精度)那么我如何解决这个问题呢?您的循环体已经正确地检查了每个元素的类型。因此,使用该逻辑初始化您的
总计
,首先将
总计设置为空:

@Override
public Double max() {
    Double total = (Double)super.array[0];
    for(T t : super.array) {
        if (t == null) {
            continue;
        }
        else if (t instanceof Number) {
            Number n = (Number)t;
            if(total < n.doubleValue()) {
                total = n.doubleValue();
            }
        }
        else if(t instanceof INumber) {
            INumber n = (INumber)t;
            if(total < n.getNumber()) {
                total = n.getNumber();
            }
        }
    }
    return total;
        
然后,您可以检查循环中的null:

Double total = null;
for(T:super.array){
如果(t==null){
继续;
}
else if(t实例数){
数字n=(数字)t;
if(total==null | | total
在第一行
Double total=(Double)super.array[0];
中,您已将元素强制转换为Double。因此任何其他类型都会引发异常,这绝对不是您想要的

事实上,如果要获取数组的max元素,该元素必须实现
Comparable
接口。您可以这样做:

for (T t : super.array) {
    if (t == null) {
        continue;
    }
    else if (t instanceof Number) {
        Number n = (Number)t;
        if (total == null || total < n.doubleValue()) {
            total = n.doubleValue();
        }
    }
    else if(t instanceof INumber) {
        INumber n = (INumber)t;
        if (total == null || total < n.getNumber()) {
            total = n.getNumber();
        }
    }
}
T getMax(T[]数组){
T max=null;
for(T:array){
如果(max==null)max=t;
否则{
max=max.compareTo(t)>0?max:t;
}
}
返回最大值;
}

Java中比较对象的一般方法是实现
Comparable
接口,该接口记录了如何将一个对象与另一个对象进行比较。
<T extends Comparable<T>> T getMax(T[] array){
        T max=null;
        for (T t : array) {
            if (max==null) max=t;
            else {
                max=max.compareTo(t)>0?max:t;
            }
        }
        return max;
    }