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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张振纲 中级黑马   /  2012-8-7 11:45  /  1224 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;

  2. class Person
  3. {
  4.         private String name;
  5.         private int age;
  6.         Person(String name, int age)
  7.         {
  8.                 this.name=name;
  9.                 this.age=age;
  10.         }
  11.         
  12.         public int getAge()
  13.         {
  14.                 return age;
  15.         }

  16.         public String getName()
  17.         {
  18.                 return name;
  19.         }
  20. }

  21. class Mycom implements Comparator<Person>
  22. {
  23.         int compareTo(Person a ,Person b)
  24.         {
  25.                 return a.getAge().compareTo(b.getAge());        
  26.         }
  27. }

  28. class  TreeSetDemo2
  29. {
  30.         public static void main(String[] args)
  31.         {
  32.                 TreeSet<Perosn> ts = new TreeSet<Perosn>(new Mycom());

  33.                 Person a = new person("dafsd",12);
  34.                 Person b = new person("sda",12);
  35.                 Person c = new person("weqd",14);
  36.                 Person d = new person("dawsd",17);
  37.                
  38.                 Iterator it = ts.iterator();
  39.                 while (it.hasNext())
  40.                 {
  41.                         System.out.println(it.next().getName());
  42.                 }
  43.         }
  44. }
复制代码


我想通过创建一个比较器来往TreeSet里存数据
如何使用泛型才能实现?

为什么会出现这个错误

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
本帖最后由 周坤 于 2012-8-7 12:14 编辑

错误貌似多了,最好自己再熟悉熟悉。
  1. import java.util.*;


  2. class Person

  3. {

  4.         private String name;

  5.         private int age;

  6.         Person(String name, int age)

  7.         {

  8.                 this.name=name;

  9.                 this.age=age;

  10.         }

  11.         

  12.         public int getAge()

  13.         {

  14.                 return age;

  15.         }


  16.         public String getName()

  17.         {

  18.                 return name;

  19.         }

  20. }


  21. class Mycom implements Comparator<Person>

  22. {

  23.         int compareTo(Person a ,Person b)[color=Red]//实现Comparator,复写的是compare()方法,而不是compareTo()方法。[/color]

  24.         {

  25.                 return a.getAge().compareTo(b.getAge());  // [color=Red]这里也不对,int型不能用compareTo,想用的话转成Integer。 return new Integer(a.getAge()).compareTo(new Integer(b.getAge()));   
  26. [/color]
  27.         }

  28. }


  29. class  TreeSetDemo2
  30. {

  31.         public static void main(String[] args)
  32.         {

  33.                 TreeSet<Perosn> ts = new TreeSet<Perosn>(new Mycom());


  34.                 Person a = new person("dafsd",12);//[color=Red]person大小写不对,new Person("dafsd",12);[/color]

  35.                 Person b = new person("sda",12);

  36.                 Person c = new person("weqd",14);

  37.                 Person d = new person("dawsd",17);

  38.                
  39.                 Iterator it = ts.iterator();//[color=Red]这里也要用泛型,否则下面就要强转。Iterator<Person> it = ts.iterator();[/color]

  40.                 while (it.hasNext())

  41.                 {

  42.                         System.out.println(it.next().getName());

  43.                 }

  44.         }

  45. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
周坤 发表于 2012-8-7 12:12
错误貌似多了,最好自己再熟悉熟悉。

太谢谢你给我改代码了

我又改了改

代码么有错误了,可是现在只能存进去两个对象,为什么?
  1. import java.util.*;

  2. class Person
  3. {
  4. private String name;
  5. private int age;
  6. Person(String name, int age)
  7. {
  8. this.name=name;
  9. this.age=age;
  10. }

  11. public int getAge()
  12. {
  13. return age;
  14. }

  15. public String getName()
  16. {
  17. return name;
  18. }
  19. }

  20. class Mycom implements Comparator<Person>
  21. {
  22. public int compare(Person a ,Person b)
  23. {
  24. int c = new Integer(a.getAge()).compareTo(new Integer (b.getAge()));
  25. if (c==0)
  26. {
  27. return a.getName().compareTo(b.getName());
  28. }
  29. return c ;
  30. }
  31. }

  32. class TreeSetDemo2
  33. {
  34. public static void main(String[] args)
  35. {
  36. TreeSet<Person> ts = new TreeSet<Person>(new Mycom());

  37. ts.add(new Person("dafsd",13));
  38. ts.add(new Person("sda",12));
  39. ts.add(new Person("weqd",14));
  40. ts.add(new Person("dawsd",17));

  41. Iterator<Person> it = ts.iterator();
  42. while (it.hasNext())
  43. {
  44. System.out.println(it.next().getName()+"-----"+it.next().getAge());
  45. }
  46. }
  47. }
复制代码
回复 使用道具 举报
存进去的是4个元素,但是你迭代的时候出了问题       
while (it.hasNext()) {
      System.out.println(it.next().getName() + "-----" + it.next().getAge());
}
next()了2次  每次都返回了一个元素

这个是输出结果,没发现和你添加进去的不对吗?
sda-----13
weqd-----17

改成:

                while (it.hasNext()) {
                        Person p = it.next();
                        System.out.println(p.getName() + "   " + p.getAge());
                }

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

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