使用泛型类型动态计算浮点数和整数

Using Generic types to calculate with floats and ints dynamically

以下代码是我需要确定数据列 minimum/maximum 的项目的一部分。问题是,我不知道该列是否包含浮点数或整数。我尝试制作一个通用的 class 来执行此操作如下,但这给了我 currentVal > compareVal 上的错误。我的问题是;我哪里出错了?

 public <T> List<DataRow> compare(boolean Int) {
    for(int i = 0; i<table.getRowCount(); i++){
        T currentVal = (T) argument.resolve(row).getValue();
        DataRow compare = table.getRow(i);
        T compareVal = (T) argument.resolve(compare).getValue();
        // if there's a new minimum or there's a new maximum
        if((currentVal > compareVal && minimum) || (currentVal < compareVal && maximum)){
            row = compare;
            rowlist.clear();
            rowlist.add(compare);
        }
        // if there's a duplicate minimum/maximum
        else if(currentVal == compareVal)
            rowlist.add(compare);
    }
    return rowlist;
}

顺便说一句,在第一个 if 语句中,最小值和最大值是布尔值,如果要计算最小值或最大值,则它们为真。

这应该是我当前方法的替代方法;

public List<DataRow> floatCompare() {
    for(int i = 0; i<table.getRowCount(); i++){
        float currentVal = (float) argument.resolve(row).getValue();
        DataRow compare = table.getRow(i);
        float compareVal = (float) argument.resolve(compare).getValue();
        // if there's a new minimum or there's a new maximum
        if((currentVal > compareVal && minimum) || (currentVal < compareVal && maximum)){
            row = compare;
            rowlist.clear();
            rowlist.add(compare);
        }
        // if there's a duplicate minimum/maximum
        else if(currentVal == compareVal)
            rowlist.add(compare);
    }
    return rowlist;
}
public List<DataRow> intCompare() {
    for(int i = 0; i<table.getRowCount(); i++){
        int currentVal = (int) argument.resolve(row).getValue();
        DataRow compare = table.getRow(i);
        int compareVal = (int) argument.resolve(compare).getValue();
        // new minimum or new maximum
        if((currentVal > compareVal && minimum) || (currentVal < compareVal && maximum)){
            row = compare;
            rowlist.clear();
            rowlist.add(compare);
        }
        // duplicate minimum/maximum
        else if(currentVal == compareVal)
            rowlist.add(compare);
    }
    return rowlist;
}

这些函数有效,但我想将它们组合成一个通用的class,因为它们做的事情完全相同。

在 Java 中,泛型仅在编译时使用以帮助确保类型正确性。为了您的目的,我建议您创建两个函数:一个用于整数,一个用于浮点数

我们可以通过Comparable界面进行通用比较。

Returns:

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

例如:

static <T extends Comparable<? super T>> T min(T lhs, T rhs) {
    // same as ( (lhs < rhs) ? lhs : rhs )
    return (lhs.compareTo(rhs) < 0) ? lhs : rhs;
}

FloatInteger 都实现了 Comparable

使用 Comparable 的代码的逐行副本将是:

public List<DataRow> compare(boolean Int) {
    for(int i = 0; i<table.getRowCount(); i++){
        Comparable currentVal = (Comparable) argument.resolve(row).getValue();
        DataRow compare = table.getRow(i);
        Comparable compareVal = (Comparable) argument.resolve(compare).getValue();
        // if there's a new minimum or there's a new maximum
        int comparison = currentVal.compareTo(compareVal);
        if((comparison > 0 && minimum) || (comparison < 0 && maximum)){
            row = compare;
            rowlist.clear();
            rowlist.add(compare);
        }
        // if there's a duplicate minimum/maximum
        else if(comparison == 0)
            rowlist.add(compare);
    }
    return rowlist;
}

但是使用 raw types 会收到警告。我不太清楚你的代码应该为我做些什么来说明这是否是一个准确的转换以及如何避免使用原始类型。

否则有no way to do arithmetic generically.