A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lxsaks 中级黑马   /  2014-12-26 13:34  /  847 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

昨天在做黑马程序员入学的基础测试题,创建一个Student类,有name ,age,score三个属性。创建5个student 对象,装入TreeSet中,要根据score排序。纠结

评分

参与人数 1黑马币 +3 收起 理由
杨佳名 + 3

查看全部评分

3 个回复

倒序浏览
自定义一个比较器,重写equals方法和hashcode方法。
回复 使用道具 举报
这个涉及到集合框架部分的知识,集合相当重要。建议自己去好好看看毕向东老师的视频。
回复 使用道具 举报
1、学生类实现Comparable接口
2、自定义比较器。
代码是毕老师视频里的代码。
可以看JAVA-API这部分第15天【25天版本的视频】中的
【01-集合框架(TreeSet)】、【02-集合框架(TreeSet存储自定义对象)】这两个视频,有详解。
如果不想看视频,就看下面的代码

  1. import java.util.*;
  2. /*
  3. 当元素自身不具备比较性,或者具备的比较性不是所需要的。
  4. 这时需要让容器自身具备比较性。
  5. 定义一个比较器,将比较器作为参数传递给TreeSet集合的构造函数。

  6. 当两种排序都存在是,以比较器为主。
  7. 定义一个类,实现Comparator接口,覆盖compare方法。

  8. 以return:0判断元素相同
  9. */
  10. class TreeSetDemo1
  11. {
  12.         public static void main(String[] args)
  13.         {
  14.                 TreeSet ts = new TreeSet(new MyCompare());
  15.                 ts.add(new Student("lisi02",22));
  16.                 ts.add(new Student("lisi007",20));
  17.                 ts.add(new Student("lisi09",19));
  18.                 ts.add(new Student("lisi01",19));
  19.                 ts.add(new Student("lisi01",29));
  20.                 Iterator it =ts.iterator();
  21.                 while(it.hasNext()){
  22.                         Student stu = (Student)it.next();
  23.                         sop("Name:"+stu.getName()+"::::age:"+stu.getAge());
  24.                        
  25.                 }
  26.         }
  27.         public static void sop(Object obj){
  28.                 System.out.println(obj);
  29.         }
  30. }

  31. class Student implements Comparable//该接口强制让学生具备比较性。
  32. {
  33.         private String name;
  34.         private int age;
  35.         Student(String name ,int age){
  36.                 this.name = name;
  37.                 this.age=age;
  38.         }

  39.         public int compareTo(Object obj){
  40.                 if(!(obj instanceof Student))
  41.                         throw new RuntimeException("不是学生对象。");
  42.                 Student stu =(Student)obj;
  43.                 //System.out.println(this.name+"<--this...........stu-->"+stu.name);
  44.                 if(this.age>stu.age){
  45.                         return 1;
  46.                 }else if(this.age<stu.age){
  47.                         return -1;
  48.                 }else{
  49.                         return this.name.compareTo(stu.name);
  50.                 }

  51.         }

  52.         public String getName(){
  53.                 return name;
  54.         }

  55.         public int getAge(){
  56.                 return age;
  57.         }
  58. }

  59. class MyCompare implements Comparator
  60. {
  61.         public int compare(Object o1,Object o2){
  62.                 Student s1 = (Student)o1;
  63.                 Student s2 = (Student)o2;
  64.                 int num = s1.getName().compareTo(s2.getName());
  65.                 if(num==0){
  66.                         return (new Integer(s1.getAge())).compareTo(new Integer(s2.getAge()));
  67.                         //return s1.getAge()-s2.getAge();
  68.                 }
  69.                 return num;
  70.         }
  71. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马