在尝试根据元素字符串长度对数组进行排序时,我遇到了编译错误.我有一套开始,
Set<String> arraycat = new HashSet<String>();
//add contents to arraycat
String[] array = arraycat.toArray(new String[0]);
//array looks like this now:
//array=[cat,cataaaa,cataa,cata,cataaa]
Run Code Online (Sandbox Code Playgroud)
我理想的是要排序到
array=[cat,cata,cataa,cataaa,cataaaa]
Run Code Online (Sandbox Code Playgroud)
所以我有一个类型的比较器
class comp implements Comparator {
public int compare(String o1, String o2) {
if (o1.length() > o2.length()) {
return 1;
} else if (o1.length() < o2.length()) {
return -1;
} else {
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我打电话给上课
Collections.sort(array, new comp());
Run Code Online (Sandbox Code Playgroud)
但是,它抛出了两个编译错误:
comp is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.Comparator
class comp …Run Code Online (Sandbox Code Playgroud) java ×1