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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 rolan 于 2015-4-30 23:47 编辑

如题,我用的TreeSet ,但是只输出一条信息,是咋回事呢??
  1. import java.util.Comparator;
  2. import java.util.Iterator;
  3. import java.util.TreeSet;


  4. public class Fanxin {
  5.     public static void main(String[] args){
  6.             TreeSet<Person> tre=new TreeSet<Person>(new comp());
  7.             tre.add(new Students("a2",23));
  8.             tre.add(new Students("a1",24));
  9.             tre.add(new Students("a3",20));
  10.             tre.add(new Students("a4",21));
  11.             tre.add(new Students("a5",27));
  12.             Iterator<Person> it=tre.iterator();
  13.             while(it.hasNext()){
  14.                     Person p=(Person)it.next();
  15.                     System.out.print(p.GetName()+"-------"+p.GetAge());
  16.             }
  17.     }
  18. }
  19. class comp implements Comparator<Person>{

  20.         public int compare(Person ob1,Person ob2) {
  21.                 return ob1.GetName().compareTo(ob1.GetName());
  22.                
  23.         }
  24.         
  25. }
  26. class Person{
  27.         private String name;
  28.         private int age;
  29.         Person(String name,int age){
  30.                 this.name=name;
  31.                 this.age=age;
  32.         }
  33.         public String GetName(){
  34.                 return name;
  35.         }
  36.         public int GetAge(){
  37.                 return age;
  38.         }
  39. }
  40. class Students extends Person{

  41.         Students(String name, int age) {
  42.                 super(name, age);
  43.                
  44.         }
  45.         
  46. }
  47. class Workers extends Person{

  48.         Workers(String name, int age) {
  49.                 super(name, age);
  50.                
  51.         }
  52.         
  53. }
复制代码

9 个回复

倒序浏览
你用的TreeSet集合,set集合不能存重复的元素,你的自定义person类没有实现comparable所以不具备比较性,无法存入集合中的。
回复 使用道具 举报
你不信把你add的那些对象换成String对象试一下,String有实现过comparable。视频中应该有讲的啊
回复 使用道具 举报
考不上黑马 发表于 2015-4-30 23:37
你用的TreeSet集合,set集合不能存重复的元素,你的自定义person类没有实现comparable所以不具备比较性,无 ...

我改了一下,还是不行,不知道是什么错误,如果正如你所说的错误,无可比性只能接收一个值,但是我存入了几个值,应该会发生编译错误才对啊,但是我这个编译没有发生错误,按照你所说的改了一下,代码如下,你看看对不对
  1. import java.util.Comparator;
  2. import java.util.Iterator;
  3. import java.util.TreeSet;


  4. public class Fanxin {
  5.     public static void main(String[] args){
  6.             TreeSet<Person> tre=new TreeSet<Person>(new comp());
  7.             tre.add(new Students("a2",23));
  8.             tre.add(new Students("a1",24));
  9.             tre.add(new Students("a3",20));
  10.             tre.add(new Students("a4",21));
  11.             tre.add(new Students("a5",27));
  12.             Iterator<Person> it=tre.iterator();
  13.             while(it.hasNext()){
  14.                     Person p=(Person)it.next();
  15.                     System.out.print(p.GetName()+"-------"+p.GetAge());
  16.             }
  17.     }
  18. }
  19. class comp implements Comparator<Person>{

  20.         public int compare(Person ob1,Person ob2) {
  21.                 return ob1.GetName().compareTo(ob1.GetName());
  22.                
  23.         }
  24.         
  25. }
  26. class Person implements Comparable{
  27.         private String name;
  28.         private int age;
  29.         Person(String name,int age){
  30.                 this.name=name;
  31.                 this.age=age;
  32.         }
  33.         public String GetName(){
  34.                 return name;
  35.         }
  36.         public int GetAge(){
  37.                 return age;
  38.         }
  39.                 public int compareTo(Object obj) {
  40.                         // TODO Auto-generated method stub
  41.                         if(!(obj instanceof Person))
  42.                                  throw new RuntimeException("传入对象非法");
  43.                              Person p=(Person)obj;
  44.                              if(this.age>p.age)
  45.                                      return 1;
  46.                              if(this.age==p.age)
  47.                                      return 0;
  48.                              return -1;
  49.                 }
  50.                
  51. }
  52. class Students extends Person{

  53.         Students(String name, int age) {
  54.                 super(name, age);
  55.                
  56.         }
  57.         
  58. }
  59. class Workers extends Person{

  60.         Workers(String name, int age) {
  61.                 super(name, age);
  62.                
  63.         }
  64.         
  65. }
复制代码


360截图20150501000146488.jpg (139.06 KB, 下载次数: 16)

360截图20150501000146488.jpg
回复 使用道具 举报
考不上黑马 发表于 2015-4-30 23:37
你用的TreeSet集合,set集合不能存重复的元素,你的自定义person类没有实现comparable所以不具备比较性,无 ...

两种方式呀,他用的是自定义比较器的方法
回复 使用道具 举报
第24行写错了return ob1.GetName().compareTo(ob1.GetName());
两个ob1了,改下就行了
另外也还有问题:
迭代器里面的的P不需要强转了
获取的方法规范的写法应该是:getAge,getName
回复 使用道具 举报 1 0
hui1130 发表于 2015-5-1 00:13
两种方式呀,他用的是自定义比较器的方法

对,看到了,他有比较器。没注意,嘿嘿
回复 使用道具 举报
hui1130 发表于 2015-5-1 00:13
两种方式呀,他用的是自定义比较器的方法

对,看到了,他有比较器。没注意,嘿嘿~~~~~~~~~
回复 使用道具 举报
hui1130 发表于 2015-5-1 00:34
第24行写错了return ob1.GetName().compareTo(ob1.GetName());
两个ob1了,改下就行了
另外也还有问题:

火眼金睛。。。
回复 使用道具 举报
hui1130 发表于 2015-5-1 00:34
第24行写错了return ob1.GetName().compareTo(ob1.GetName());
两个ob1了,改下就行了
另外也还有问题:

哇哇。。。。谢谢大神。。赞赞赞。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马