public void setName(String name) { this.name = name; }
public int getScore() { return score; }
public void setScore(int score) { this.score = score; }
public Student(String name, int score) { super(); this.name = name; this.score = score; }
//复写compareto方法 @Override public int compareTo(Student s) { if (this.score > s.score) { return -1; } else if(this.score < s.score) { return 1; }else { return 0; } }
}
public class Compare { public static void main(String[] args) { Student[] arrs={ new Student("李四",66), new Student("张三",58), new Student("Dick", 100), new Student("王五", 90), new Student("小刘", 77), }; Arrays.sort(arrs);//比较两个对象,成绩最优的排名最前 for (int i = 0; i < arrs.length; i++) { System.out.println(arrs[i]); } } }