統計データを含むタイプ 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);
}
}
}
私の質問は、このメソッドをジェネリックにして、Float 型の ArrayLists を受け入れて計算できるだけでなく、Integers なども実行できるようにする方法です。次のようなテンプレートを使用しようとしました。
ArrayList<? as Number>
しかし Collections.sort にたどり着くと文句が出て、何が悪いのかわかりませんでした。戻り値は文字列である必要があります。