黑马程序员技术交流社区

标题: 关于TreeSet 求帮助!!!!!!!! [打印本页]

作者: 刘茂林    时间: 2013-5-14 10:00
标题: 关于TreeSet 求帮助!!!!!!!!
本帖最后由 刘茂林 于 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. }         
复制代码
一直出现问题 没照出来哪里错了 多谢帮助了
作者: 王春晓    时间: 2013-5-14 10:15
本帖最后由 王春晓 于 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. }
复制代码

作者: 尖卡斌引    时间: 2013-5-14 10:16
你定义的是Student类,但是你在向你存的时候你new了以个Person对象(第十九行);
你在程序中没有定义Person类;出错
你运行下看看提示“ 没有找到符号Person();”
把Person (第19行)改为Student即可。
作者: up.yfei    时间: 2013-5-14 11:21
你new 的是Person  ?   要是对了就奇怪了 ......
作者: 刘胜寒    时间: 2013-5-14 11:48
楼主可以结贴了。。。
有木有
作者: kaka小明    时间: 2013-5-14 12:36
两个问题 1:Treeset未定义泛型,2类未找到。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2