| 本帖最后由 李振元 于 2011-12-13 16:22 编辑 
 我在记事本里编写了一下,希望对楼主有所帮助:
 
 import java.util.*;
 class TreeSetTest
 {
 public static void main(String[] args)
 {
 TreeSet ts = new TreeSet();
 
 ts.add(new Student("lisi01",11,"男",99));
 ts.add(new Student("lisi01",13,"女",94));
 ts.add(new Student("lisi03",24,"男",95));
 ts.add(new Student("lisi04",27,"女",98));
 ts.add(new Student("lisi05",11,"男",97));
 
 Iterator it =ts.iterator();
 while(it.hasNext())
 {
 Student stu = (Student)it.next();
 System.out.println(stu.getName()+"..."+stu.getAge()
 +"..."+stu.getSex()+"..."+"..."+stu.getGrade());
 }
 }
 }
 
 class Student implements Comparable
 {
 private String name;
 private int age;
 private String sex;
 private int grade;
 Student(String name,int age,String sex,int grade)
 {
 this.name = name;
 this.age = age;
 this.sex = sex;
 this.grade = grade;
 }
 public int compareTo(Object obj)
 {
 if(!(obj instanceof Student))
 throw new RuntimeException("不是学生类");
 Student s = (Student)obj;
 if(this.grade>s.grade)
 return 1;
 if(s.grade==this.grade)
 {
 return this.name.compareTo(s.name);
 }
 return -1;
 
 
 }
 
 //都只提供get()方法,楼主可以自己增加set()方法
 public String getName()
 {
 return name;
 }
 public int getAge()
 {
 return age;
 }
 public String getSex()
 {
 return sex;
 }
 public int getGrade()
 {
 return grade;
 }
 
 }
 |