Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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 使用泛型类型动态计算浮点和整数_Java_Generics_Generic Programming - Fatal编程技术网

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

Java 使用泛型类型动态计算浮点和整数,java,generics,generic-programming,Java,Generics,Generic Programming,下面的代码是我需要确定数据列的最小/最大值的项目的一部分。问题是,我不知道该列将包含float还是int。我试图创建一个泛型类来实现这一点,如下所示,但这给了我currentVal>compareVal上的错误。我的问题是,;我哪里出错了 public <T> List<DataRow> compare(boolean Int) { for(int i = 0; i<table.getRowCount(); i++){ T currentV

下面的代码是我需要确定数据列的最小/最大值的项目的一部分。问题是,我不知道该列将包含float还是int。我试图创建一个泛型类来实现这一点,如下所示,但这给了我
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;
}
公共列表比较(布尔整数){
对于(int i=0;i比较值和最小值)| |(currentVal<比较值和最大值)){
行=比较;
rowlist.clear();
行列表。添加(比较);
}
//如果存在重复的最小值/最大值
else if(currentVal==compareVal)
行列表。添加(比较);
}
返回行列表;
}
顺便说一句,在第一个if语句中,如果要计算最小值或最大值,那么最小值和最大值都是布尔值

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

public List floatCompare(){
对于(int i=0;i比较值和最小值)| |(currentVal<比较值和最大值)){
行=比较;
rowlist.clear();
行列表。添加(比较);
}
//如果存在重复的最小值/最大值
else if(currentVal==compareVal)
行列表。添加(比较);
}
返回行列表;
}
公共列表intCompare(){
对于(int i=0;i比较值和最小值)| |(currentVal<比较值和最大值)){
行=比较;
rowlist.clear();
行列表。添加(比较);
}
//重复最小值/最大值
else if(currentVal==compareVal)
行列表。添加(比较);
}
返回行列表;
}

这些函数可以工作,但我想将它们组合成一个泛型类,因为它们都做完全相同的事情。

在Java中,泛型只在编译时使用,以帮助确保类型正确性。出于您的目的,我建议您创建两个函数:一个用于int,另一个用于float

我们可以通过接口进行一般比较

返回:

负整数、零或正整数,因为此对象小于、等于或大于指定对象

例如:

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


否则,就有了。

它们真的是int/float还是Integer/float?argument.resolve(row).getValue()返回什么?这个变量来自哪里?你的代码没有编译,你能修复它吗?我知道它没有编译,对不起。它在包含数据行和数据列结构的大型后端上运行。argument.resolve(row).getValue()返回一个值,该值可以是浮点值,也可以是整数。Never和Integer将问题归结为一个显示您需要执行的操作的值。目前,我们没有足够的信息为您提供准确的建议。为什么它们是float或int有关系?你不会用同样的方式来比较吗?正是我所想的,但我的项目团队不同意。我编辑了我的帖子,以展示我对你的建议的实施情况。这对你的团队来说太糟糕了,没有其他办法。哇,你的逐行复制正是我所需要的。尽管我的解释很糟糕,但你还是设法解决了这个问题,如果可能的话,我会给你一千张投票。不过,请一定阅读我链接的原始类型问答。考虑以不允许使用它们的方式重构它。(但原始可比数据并不像其他一些原始数据类型那样糟糕:compareTo的声明应该使其立即抛出异常,而不是允许堆污染。收集框架在幕后常规使用原始可比数据…)
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;
}
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;
}
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;
}