我弄个例子给你吧,稍等下import java.util.*;
public class Student implements Comparable{
private double mark;
private String name;
public Student(String name,double mark){
this.name=name;
this.mark=mark;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(!(o instanceof Student)) return 1;
Student tmp=(Student)o;
if(this.getMark()>tmp.getMark()) return 1;
if(this.getMark()
return 0;
}
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String args[]){
List list=new ArrayList();
for(int i=0;i<10;i++){
list.add(new Student("S_NAME_"+i,Math.random()*100));
}
for(Student s:list){
System.out.print(s.getName()+"-"+s.getMark()+"\t");
}
System.out.println();
Collections.sort(list);
for(Student s:list){
System.out.print(s.getName()+"-"+s.getMark()+"\t");
}
}
}
comparable接口是用来比较用的。你看看我这个例子吧,不懂就追问