日期:2014-05-20 浏览次数:20870 次
import java.util.ArrayList;
import java.util.Collections;
public class StudentRanging {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
Student a = new Student("胖胖", 30, 90.5);
Student b = new Student("刘静", 25, 92.5);
Student c = new Student("苏凯多多", 21, 80.5);
Student f = new Student("阿成才", 24, 80.5);
Student d = new Student("许三多", 28, 90.5);
Student e = new Student("成才", 20, 90.5);
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
list.add(f);
Collections.sort(list);
for (Student s : list) {
System.out.println("info:" + s);
}
}
public static class Student implements Comparable<Student> {
String name;
int age;
double score;
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getScore() {
return score;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return name + " " + age + " " + score;
}
@Override
public int compareTo(Student o) {
int result = 0;
return this.getName().compareTo(o.getName());
}
}
}