我尝试创建一个集合,只有3个名称,并希望按字母顺序对它们进行排序。但总是出现错误:Exception in thread "main" java.lang.AbstractMethodError: collection.compareTo(Ljava/lang/Object;)I 在此行:Collections.sort(names); 我应该怎么做才能解决这个问题?
public class collection implements Comparable<collection> {
private String name;
public collection(String name){
this.name= name;
}
public String getName(){
return name;
}
public int compareΤο(collection c){
return this.getName().compareTo(c.getName());
}
}
public class collectionList {
private ArrayList <collection> names;
public collectionList(){
names = new ArrayList <collection>();
}
public void populate() {
collection c1 = new collection("Monica Rows");
names.add(c1);
collection c2 = new collection("Peter Walker");
names.add(c2);
collection c3 = new collection("Jack Miller");
names.add(c3);
}
public void sortBy(){
Collections.sort(names);
}
public String names(){
String s="";
for(collection c: names){
s+=c.getName()+ "\n";
}
return s;
}
}
public class collectionMain {
public static void main(String[] args){
collectionList c = new collectionList();
c.populate();
System.out.println(c.names());
c.sortBy();
System.out.println(c.names());
}
}