Java 扩展泛型类型的方法签名”;“不正确”;
我不理解java泛型的编译时错误 我有两种方法:Java 扩展泛型类型的方法签名”;“不正确”;,java,generics,Java,Generics,我不理解java泛型的编译时错误 我有两种方法: public static final <T> T doStuff(List<T> list, int k, Comparator<T> comparator) { T element = null; //some stuff done return element; } public static final <T extends Comparable> T doStuff(List
public static final <T> T doStuff(List<T> list, int k, Comparator<T> comparator) {
T element = null;
//some stuff done
return element;
}
public static final <T extends Comparable> T doStuff(List<T> list, int k) {
Comparator<T> comparator = new Comparator<T>() {
public int compare (T o1, T o2) {
return o1.compareTo(o2);
}
};
return doStuff(list, k, comparator);
}
public static final T doStuff(列表、int k、比较器){
T元素=null;
//做了些什么
返回元素;
}
公共静态最终温度(列表,int k){
比较器比较器=新比较器(){
公共整数比较(tO1,tO2){
返回o1。与(o2)相比;
}
};
返回doStuff(列表、k、比较器);
}
由于找不到方法doStuff(java.util.List,int,java.util.Comparator),此操作失败。
当我将第二种方法更改为:
public static final <T> T doStuff(List<T> list, int k) {
Comparator<T> comparator = new Comparator<T>() {
public int compare (T o1, T o2) {
return ((Comparable)o1).compareTo(o2);
}
};
return doStuff(list, k, comparator);
}
public static final T doStuff(列表,int k){
比较器比较器=新比较器(){
公共整数比较(tO1,tO2){
回报率((可比)o1),与(o2)相比;
}
};
返回doStuff(列表、k、比较器);
}
它很好用
有人能解释一下吗。Comparable本身就是一个泛型。如果你改为
public static final <T extends Comparable<? super T>> T doStuff(List<T> list, int k) ...
publicstaticfinalcompariable本身就是一个泛型。如果你改为
public static final <T extends Comparable<? super T>> T doStuff(List<T> list, int k) ...
publicstaticfinalcompariable本身就是一个泛型。如果你改为
public static final <T extends Comparable<? super T>> T doStuff(List<T> list, int k) ...
publicstaticfinalcompariable本身就是一个泛型。如果你改为
public static final <T extends Comparable<? super T>> T doStuff(List<T> list, int k) ...
publicstaticfinal您的泛型参数由原始类型Comparable
限定,该类型与自身不可比(即T
)
相反,请将类型从
更改为
编译以下代码:
public static final <T> T doStuff(List<T> list, int k,
Comparator<T> comparator) {
T element = null;
// some stuff done
return element;
}
public static final <T extends Comparable<T>> T doStuff(List<T> list, int k) {
Comparator<T> comparator = new Comparator<T>() {
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
};
return doStuff(list, k, comparator);
}
public static final T doStuff(列表,int k,
比较器(比较器){
T元素=null;
//做了些什么
返回元素;
}
公共静态最终温度(列表,int k){
比较器比较器=新比较器(){
公共整数比较(tO1,tO2){
返回o1。与(o2)相比;
}
};
返回doStuff(列表、k、比较器);
}
您的泛型参数由原始类型Comparable
限定,该类型与自身不可比(即T
)
相反,请将类型从
更改为
编译以下代码:
public static final <T> T doStuff(List<T> list, int k,
Comparator<T> comparator) {
T element = null;
// some stuff done
return element;
}
public static final <T extends Comparable<T>> T doStuff(List<T> list, int k) {
Comparator<T> comparator = new Comparator<T>() {
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
};
return doStuff(list, k, comparator);
}
public static final T doStuff(列表,int k,
比较器(比较器){
T元素=null;
//做了些什么
返回元素;
}
公共静态最终温度(列表,int k){
比较器比较器=新比较器(){
公共整数比较(tO1,tO2){
返回o1。与(o2)相比;
}
};
返回doStuff(列表、k、比较器);
}
您的泛型参数由原始类型Comparable
限定,该类型与自身不可比(即T
)
相反,请将类型从
更改为
编译以下代码:
public static final <T> T doStuff(List<T> list, int k,
Comparator<T> comparator) {
T element = null;
// some stuff done
return element;
}
public static final <T extends Comparable<T>> T doStuff(List<T> list, int k) {
Comparator<T> comparator = new Comparator<T>() {
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
};
return doStuff(list, k, comparator);
}
public static final T doStuff(列表,int k,
比较器(比较器){
T元素=null;
//做了些什么
返回元素;
}
公共静态最终温度(列表,int k){
比较器比较器=新比较器(){
公共整数比较(tO1,tO2){
返回o1。与(o2)相比;
}
};
返回doStuff(列表、k、比较器);
}
您的泛型参数由原始类型Comparable
限定,该类型与自身不可比(即T
)
相反,请将类型从
更改为
编译以下代码:
public static final <T> T doStuff(List<T> list, int k,
Comparator<T> comparator) {
T element = null;
// some stuff done
return element;
}
public static final <T extends Comparable<T>> T doStuff(List<T> list, int k) {
Comparator<T> comparator = new Comparator<T>() {
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
};
return doStuff(list, k, comparator);
}
public static final T doStuff(列表,int k,
比较器(比较器){
T元素=null;
//做了些什么
返回元素;
}
公共静态最终温度(列表,int k){
比较器比较器=新比较器(){
公共整数比较(tO1,tO2){
返回o1。与(o2)相比;
}
};
返回doStuff(列表、k、比较器);
}
以上代码按原样编译。。您只会收到有关原始类型比较器的用户的编译器警告(使用-Xlint选项)。上面的代码按原样编译。。您只会收到有关原始类型比较器的用户的编译器警告(使用-Xlint选项)。上面的代码按原样编译。。您只会收到有关原始类型比较器的用户的编译器警告(使用-Xlint选项)。上面的代码按原样编译。。您只会得到一个关于原始类型比较器的用户的编译器警告(使用-Xlint选项)。错误(1113,10):在类中找不到方法doStuff(java.util.List,int,java.util.Comparator)…错误(1113,10):在类中找不到方法doStuff(java.util.List,int,java.util.Comparator)…错误(1113,10):方法doStuff(java.util.List,int,java.util.Comparator)在类中找不到…错误(1113,10):方法doStuff(java.util.List,int,java.util.Comparator)在类中找不到…它仍然不在这里。我正在使用JDK 1.5.022。是否将我的代码复制粘贴到IDE?即使使用1.5,它也应该编译?是的,我得到错误:方法doStuff(java.util.List,int,java.util.Comparator)在类中找不到…为了获得最佳结果,请使用它仍然不在这里。我使用的是JDK 1.5.022。是否将我的代码复制粘贴到IDE?即使使用1.5,它也应该编译是的,我得到错误:方法doStuff(java.util.List,int,java.util.Comparator)在类中找不到…为了获得最佳结果,请使用它仍然不在这里。我正在使用JDK 1.5.0\u 22。是否将我的代码复制粘贴到IDE?即使使用1.5,它也应该编译是的,我得到错误:方法doStuff(java.util.List,int,java.util.Comparator)在类中找不到…为了获得最佳结果,请使用它仍然不在这里。我正在使用JDK 1.5.022。是否将我的代码复制粘贴到IDE?即使使用1.5,它也应该编译。是的,我得到错误:在类中找不到方法doStuff(java.util.List,int,java.util.Comparator)…为了获得最佳结果,请使用