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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 老外 中级黑马   /  2015-2-7 16:13  /  4938 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;

  2. class StuNameComparator implements Comparator<Student>
  3. {
  4.         public int compare(Student s1,Student s2)
  5.         {
  6.                 int num = s1.getName().compareTo(s2.getName());
  7.                 if(num==0)
  8.                         return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  9.                 return num;
  10.         }
  11. }

  12. class MapTest2
  13. {
  14.         public static void main(String[] args)
  15.         {
  16.                 TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());

  17.                 tm.put(new Student("lisi1",21),"beijing");
  18.                 tm.put(new Student("blisi2",22),"shanghai");
  19.                 tm.put(new Student("lisi3",23),"nanjing");
  20.                 tm.put(new Student("alisi4",24),"wuhan");

  21.                 Set<Map.Entry<Student,String>> entrySet = tm.entrySet();
  22.                 Iterator<Map.Entry<Student,String>> it = entrySet.iterator();

  23.                 while (it.hasNext())
  24.                 {
  25.                         Map.Entry<Student,String> me = it.next();
  26.                         Student stu = me.getKey();
  27.                         String addr = me.getValue();
  28.                         System.out.println(stu+":::"+addr);
  29.                 }
  30.         }
  31. }
复制代码


错误: 无法取消引用int
  int num = s1.getName().compareTo(s2.getName());

求大神帮忙!

6 个回复

倒序浏览
继承Comparator接口,实现copare方法。
public int compare(Object obj1,Object obj2)  ,要必须参数也是Object类型,然后在函数内强制转换下,你试试

点评

指定泛型,也就是你要比较的类型  发表于 2015-2-7 17:36
回复 使用道具 举报
请问,这个问题怎么解决的?
回复 使用道具 举报
牛人啊!好好学!
回复 使用道具 举报
好好学!
回复 使用道具 举报
我也出现了这个无法取消引用Int类型的错误,不过不明白。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马