问题描述
classComByLenimplementsComparator<String>{publicintcompare(Strings1,Strings2){inttemp=s1.length()-s2.length();returntemp==0?s1.compareTo(s2):temp;}}classComByLen<String>implementsComparator<String>{publicintcompare(Strings1,Strings2){inttemp=s1.length()-s2.length();returntemp==0?s1.compareTo(s2):temp;}}下面的代码会出现问题,请问是什么原因?
解决方案
解决方案二:
会出什么问题?
解决方案三:
能通过编译吗?你在定义类的时候要定义泛型应该是形如T之类的而实际的类型是在运行时传进去的~
解决方案四:
ComByLen已被定义泛型檫除即CombyLen<String>经过檫除后变为ComByLen所以会出现ComByLen已定义错误
解决方案五:
这有什么奇怪的,本来就这样,你怎么能用2个同样的类名去实现同一个接口呢
解决方案六:
问题描述不清晰
解决方案七:
抱歉,我没说清楚问题;下面是源代码[code=javaimportjava.util.List;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;publicclassCollectionsDemo2{publicstaticvoidmain(String[]args){demo1();}publicstaticvoiddemo1(){List<String>list=newArrayList<String>();list.add("ags");list.add("edn");list.add("afdb");list.add("adsn");list.add("ab");System.out.println(list);mySort(list);//Collections.sort(list);System.out.println(list);mySort2(list,newComByLen());//Collections.sort(list,newComByLen());System.out.println(list);}publicstatic<T>voidmySort2(List<T>list,Comparator<?superT>com){for(inti=0;i<list.size()-1;i++){for(intj=i;j<list.size();j++){if(com.compare(list.get(i),list.get(j))>0){Ttemp=list.get(i);list.set(i,list.get(j));list.set(j,temp);}}}}publicstatic<TextendsComparable<?superT>>voidmySort(List<T>list){for(inti=0;i<list.size()-1;i++){for(intj=i;j<list.size();j++){if(list.get(i).compareTo(list.get(j))>0){Ttemp=list.get(i);list.set(i,list.get(j));list.set(j,temp);}}}}}classComByLen<String>implementsComparator<String>{publicintcompare(Strings1,Strings2){inttemp=s1.length()-s2.length();returntemp==0?s1.compareTo(s2):temp;}}][/code]----------JAVAC----------CollectionsDemo2.java:52:错误:找不到符号inttemp=s1.length()-s2.length();^符号:方法length()位置:类型为String的变量s1其中,String是类型变量:String扩展已在类ComByLen中声明的ObjectCollectionsDemo2.java:52:错误:找不到符号inttemp=s1.length()-s2.length();^符号:方法length()位置:类型为String的变量s2其中,String是类型变量:String扩展已在类ComByLen中声明的ObjectCollectionsDemo2.java:53:错误:找不到符号returntemp==0?s1.compareTo(s2):temp;^符号:方法compareTo(String)位置:类型为String的变量s1其中,String是类型变量:String扩展已在类ComByLen中声明的Object注:CollectionsDemo2.java使用了未经检查或不安全的操作。注:有关详细信息,请使用-Xlint:unchecked重新编译。3个错误输出完成(耗时1秒)-正常终止
解决方案八:
importjava.util.List;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;publicclassCollectionsDemo2{publicstaticvoidmain(String[]args){demo1();}publicstaticvoiddemo1(){List<String>list=newArrayList<String>();list.add("ags");list.add("edn");list.add("afdb");list.add("adsn");list.add("ab");System.out.println(list);mySort(list);//Collections.sort(list);System.out.println(list);mySort2(list,newComByLen());//Collections.sort(list,newComByLen());System.out.println(list);}publicstatic<T>voidmySort2(List<T>list,Comparator<?superT>com){for(inti=0;i<list.size()-1;i++){for(intj=i;j<list.size();j++){if(com.compare(list.get(i),list.get(j))>0){Ttemp=list.get(i);list.set(i,list.get(j));list.set(j,temp);}}}}publicstatic<TextendsComparable<?superT>>voidmySort(List<T>list){for(inti=0;i<list.size()-1;i++){for(intj=i;j<list.size();j++){if(list.get(i).compareTo(list.get(j))>0){Ttemp=list.get(i);list.set(i,list.get(j));list.set(j,temp);}}}}}classComByLen<String>implementsComparator<String>{publicintcompare(Strings1,Strings2){inttemp=s1.length()-s2.length();returntemp==0?s1.compareTo(s2):temp;}}
----------JAVAC----------CollectionsDemo2.java:52:错误:找不到符号inttemp=s1.length()-s2.length();^符号:方法length()位置:类型为String的变量s1其中,String是类型变量:String扩展已在类ComByLen中声明的ObjectCollectionsDemo2.java:52:错误:找不到符号inttemp=s1.length()-s2.length();^符号:方法length()位置:类型为String的变量s2其中,String是类型变量:String扩展已在类ComByLen中声明的ObjectCollectionsDemo2.java:53:错误:找不到符号returntemp==0?s1.compareTo(s2):temp;^符号:方法compareTo(String)位置:类型为String的变量s1其中,String是类型变量:String扩展已在类ComByLen中声明的Object注:CollectionsDemo2.java使用了未经检查或不安全的操作。注:有关详细信息,请使用-Xlint:unchecked重新编译。3个错误输出完成(耗时1秒)-正常终止