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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘茂林 高级黑马   /  2013-5-14 10:00  /  1272 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 刘茂林 于 2013-5-14 11:54 编辑
  1. import java.util.*;
  2. import java.lang.*;
  3. /*
  4. * 实现在容器TreeSet中存入自定义对象
  5. *
  6. * 比如存个学生  学生有姓名年龄属性 并按照年龄进行排序
  7. * */

  8. public class TreeSetDemo
  9. {
  10.     public static void sop(Object obj)
  11.     {
  12.         System.out.println(obj);
  13.     }
  14.    
  15.     public static void main(String[] args)
  16.     {
  17.         TreeSet ts = new TreeSet();
  18.         ts.add(new Person("lisi02",22));//
  19.         //ts.add(new Person("lisi007",27));
  20.         //ts.add(new Person("lisi05",28));
  21.         //ts.add(new Person("lisi09",21));
  22.         
  23.         Iterator it = ts.iterator();
  24.         while(it.hasNext())
  25.         {
  26.             Student stu = (Student)it.next();
  27.             System.out.println(stu.getName() + "......" + stu.getAge());
  28.         }

  29.     }

  30. }

  31. class Student implements Comparable//该接口让学生具有比较性
  32. {
  33.     private String name;
  34.     private int age;
  35.     Student(String name, int age)
  36.     {
  37.         this.name = name;
  38.         this.age = age;
  39.     }
  40.    
  41.     public String getName()
  42.     {
  43.         return name;
  44.     }
  45.    
  46.     public int getAge()
  47.     {
  48.         return age;
  49.     }

  50.     public  int compareTo(Object obj)//必须重写comparTo方法
  51.     {
  52.         if(!(obj instanceof Student))
  53.             throw new RuntimeException("不是学生对象");
  54.         
  55.         Student s = (Student)obj;
  56.         
  57.         if(this.age > s.age)
  58.             return 1;
  59.         if(this.age == s.age)
  60.             return 0;
  61.         return -1;
  62.     }
  63. }         
复制代码
一直出现问题 没照出来哪里错了 多谢帮助了

5 个回复

倒序浏览
本帖最后由 王春晓 于 2013-5-14 10:18 编辑
  1. import java.util.*;
  2. /*
  3. * 实现在容器TreeSet中存入自定义对象
  4. *
  5. * 比如存个学生  学生有姓名年龄属性 并按照年龄进行排序
  6. * */

  7. public class TreeSetDemo
  8. {
  9.     public static void sop(Object obj)
  10.     {
  11.         System.out.println(obj);
  12.     }
  13.    
  14.     public static void main(String[] args)
  15.     {
  16.         TreeSet<Student> ts = new TreeSet<Student>();
  17.         ts.add(new Student("lisi02",22));//@@@@@@@@@这里的类名写错了!
  18.         //ts.add(new Person("lisi007",27));
  19.         //ts.add(new Person("lisi05",28));
  20.         //ts.add(new Person("lisi09",21));
  21.         
  22.         Iterator<Student> it = ts.iterator();
  23.         while(it.hasNext())
  24.         {
  25.             Student stu = (Student)it.next();
  26.             System.out.println(stu.getName() + "......" + stu.getAge());
  27.         }
  28.     }
  29. }

  30. class Student implements Comparable<Object>//该接口让学生具有比较性
  31. {
  32.     private String name;
  33.     private int age;
  34.     Student(String name, int age)
  35.     {
  36.         this.name = name;
  37.         this.age = age;
  38.     }
  39.     public String getName()
  40.     {
  41.         return name;
  42.     }
  43.     public int getAge()
  44.     {
  45.         return age;
  46.     }

  47.     public  int compareTo(Object obj)//必须重写comparTo方法
  48.     {
  49.         if(!(obj instanceof Student))
  50.             throw new RuntimeException("不是学生对象");
  51.         Student s = (Student)obj;

  52.         if(this.age > s.age)
  53.             return 1;
  54.         if(this.age == s.age)
  55.             return 0;
  56.         return -1;
  57.     }
  58. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
刘胜寒 + 1

查看全部评分

回复 使用道具 举报
你定义的是Student类,但是你在向你存的时候你new了以个Person对象(第十九行);
你在程序中没有定义Person类;出错
你运行下看看提示“ 没有找到符号Person();”
把Person (第19行)改为Student即可。

评分

参与人数 1技术分 +1 收起 理由
刘胜寒 + 1

查看全部评分

回复 使用道具 举报
你new 的是Person  ?   要是对了就奇怪了 ......
回复 使用道具 举报
楼主可以结贴了。。。
有木有
回复 使用道具 举报
两个问题 1:Treeset未定义泛型,2类未找到。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马