简体   繁体   English

如何使该Java方法完全通用

[英]How can I make this Java method completely generic

I have a method which calculates the 95th percentile of an ArrayList of type Float containing statistics data: 我有一个方法可以计算包含统计数据的Float类型的ArrayList的第95个百分位数:

    public String calculatePercentile(ArrayList<Float> arrayOfStats) {
    this.statistics = (ArrayList<Float>) arrayOfStats.clone();
    int N = statistics.size();
    int integerPart =0;
    int fractionalPart =0;
    int fraction =0;
    float rank =0;
    float floatPoint =0;
    float interpolate=0;
    float interpolateFirstPart =0;
    float interpolateSecondPart =0;

      if (N == 0) {
        return Float.toString(0);
       } else {   
         Collections.sort(statistics);
         rank = (float) ((P / 100.0) * (N - 1));
            if (rank == Math.round(rank)) {
              return Float.toString(statistics.get((int) rank));               
            } else {
                String split = Float.toString(rank);
                Pattern pattern = Pattern.compile(floatRegExPattern);
                Matcher matcher = pattern.matcher(split); 
                while(matcher.find()) {
                integerPart = Integer.parseInt(matcher.group(1));
                fractionalPart = Integer.parseInt(matcher.group(3));
                }

                if (fractionalPart < 10) {
                    floatPoint = (float) (fractionalPart / 10);
                } else {
                    floatPoint = (float) fractionalPart / 100;
                }        
         fraction = integerPart + 1;
         interpolateFirstPart = statistics.get(fraction);
         interpolateSecondPart = statistics.get(integerPart);
         interpolate = interpolateFirstPart - interpolateSecondPart;
         return roundToTwoDecimalPlaces((floatPoint * interpolate) + interpolateFirstPart);
       }
      }
}

My question is how can I make this method generic so that it can not just accept and calculate ArrayLists of type Float, but it can also do Integers etc. I have tried to use templates such as 我的问题是如何使该方法通用,以便它不仅可以接受和计算Float类型的ArrayList,而且还可以进行Integers等操作。我尝试使用诸如

ArrayList<? as Number> 

but when I get to Collections.sort, it complains and I couldn't figure out what was wrong. 但是当我进入Collections.sort时,它抱怨了,我无法弄清楚出了什么问题。 The return value needs to be a string. 返回值必须是一个字符串。

You can use 您可以使用

public <T extends Number> String calculatePercentile(ArrayList<T> arrayOfStats) {

and then use Number.floatValue to retrieve each stat as a float . 然后使用Number.floatValue检索每个统计信息作为float You'll probably have to use this to write your own comparator to do the sorting. 您可能必须使用它来编写自己的比较器来进行排序。 A comparator might look like this: 比较器可能如下所示:

Comparator<Number> c = new Comparator<Number>() {
    public int compare(Number a, Number b) {
        return Float.compare(a.floatValue(), b.floatValue());
    }
}

Incidentally, a better way to assign to statistics would be: 顺便说一句,分配给statistics的更好方法是:

this.statistics = new ArrayList<Number>(arrayOfStats);

You don't need to clone and cast. 您无需克隆和投射。

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

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