【发布时间】:2015-06-08 07:28:20
【问题描述】:
与that问题有关。
我知道通配符捕获。例如,以下内容可用于反转列表:
public static void reverse(List<?> list) { rev(list); } //capturing the wildcard
private static <T> void rev(List<T> list) {
List<T> tmp = new ArrayList<T>(list);
for (int i = 0; i < list.size(); i++) {
list.set(i, tmp.get(list.size()-i-1));
}
}
现在我正在尝试为这种情况写同样的东西:
private int compare (Comparable<?> upper, Comparable<?> lower){
return comp(upper, lower); //The method comp(Comparable<T>, Comparable<T>) is not applicable for the arguments (Comparable<capture#5-of ?>, Comparable<capture#6-of ?>)
}
private <T> int comp(Comparable<T> upper, Comparable<T> lower){
return upper.compareTo((T) lower);
}
我希望它也能很好地编译。是否可以通过这种方式为具有两个或多个参数的方法捕获通配符?
【问题讨论】: