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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 崔一恒 中级黑马   /  2013-7-21 20:38  /  1029 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 崔一恒 于 2013-7-22 19:24 编辑
  1. import java.util.*;
  2. class Student
  3. {
  4.         private String name;
  5.         private int age;
  6.         private int scores;

  7.         Student(String name,int age,int scores)
  8.         {
  9.                 this.name = name;
  10.                 this.age = age;
  11.                 this.scores=scores;
  12.         }



  13.         public String getName()
  14.         {
  15.                 return name;

  16.         }
  17.         public int getAge()
  18.         {
  19.                 return age;
  20.         }
  21.         public int getScores()
  22.         {
  23.                 return scores;
  24.         }
  25. }
  26. class Test10
  27. {
  28.         public static void main(String[] args)
  29.         {
  30.                 TreeSet ts = new TreeSet(new MyCompare());

  31.                 ts.add(new Student("lisi",22,66));
  32.                 ts.add(new Student("llinghu",21,87));
  33.                 ts.add(new Student("lliuhua",20,96));
  34.                 ts.add(new Student("lxiaolei",19,88));
  35.                 ts.add(new Student("lzhangsan",18,78));

  36.                 Iterator it = ts.iterator();
  37.                 while(it.hasNext())
  38.                 {
  39.                         Student stu = (Student)it.next();
  40.                         System.out.println(stu.getName()+"..."+stu.getAge()+"---"+stu.getScores());
  41.                 }
  42.         }
  43. }

  44. class MyCompare implements Comparator
  45. {
  46.         public int compare(Object o1,Object o2)
  47.         {
  48.                 Student s1 = (Student)o1;
  49.                 Student s2 = (Student)o2;

  50.                 int num = s1.getScores().compareTo(s2.getScores());
  51.                 if(num==0)
  52.                 {

  53.                         int num1= new Integer(s1.getName()).compareTo(new Integer(s2.getName()));
  54.                         if(num1==0){
  55.                         return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  56.                         return num1;
  57.                         }

  58.                 }

  59.                
  60.                 return num;

  61.         }
  62. }
复制代码
运行该代码提醒无法取消引用int[int num = s1.getScores().compareTo(s2.getScores());]

评分

参与人数 1技术分 +1 收起 理由
杜光 + 1 每天提问并回答问题,是对知识的复习和积累.

查看全部评分

10 个回复

倒序浏览
int num1= new Integer(s1.getName()).compareTo(new Integer(s2.getName()));
这句话有问题。Name是字符串。直接用s1.getName().compareTo(s2.getName());这样就行了。
回复 使用道具 举报
楼主这个是黑马基础测试的最后一题吧?
int num1= new Integer(s1.getName()).compareTo(new Integer(s2.getName()));//这句有问题 字符串是不能转成 Integer 类的
就算这句改过来了也只能按照成绩排序;不能实现楼主想的 先按照成绩排名,成绩相同再按照姓名排序,姓名相同再按照年龄排序的功能;
把if语句里面改一下
  1. if(num==0)
  2.                 {

  3.                         num= s1.getName().compareTo(s2.getName());
  4.                         if(num1==0){
  5.                               num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  6.                         }

  7.                 }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1 赞一个!

查看全部评分

回复 使用道具 举报
王靖远 发表于 2013-7-21 20:47
int num1= new Integer(s1.getName()).compareTo(new Integer(s2.getName()));
这句话有问题。Name是字符串 ...

你应该没运行吧  int num = s1.getScores().compareTo(s2.getScores());是这句出的问题
回复 使用道具 举报
这个是比较器,是按字符串长度进行比较的排序的。我个人认为整形是不可以比较的,不知道对不对。
呵呵,望高手指教。
回复 使用道具 举报
张强1 发表于 2013-7-21 22:52
这个是比较器,是按字符串长度进行比较的排序的。我个人认为整形是不可以比较的,不知道对不对。
呵呵,望 ...

compareTo可以比较 Integer 类的整数,不能比较 int 型的整数;
你可以查看帮助文档 Integer 类,它有compareTo 方法;
而且它比较字符串的时候不是比较长度,而是依次比较字符串的ASC码。
回复 使用道具 举报
冒烟的芒果 发表于 2013-7-22 00:17
compareTo可以比较 Integer 类的整数,不能比较 int 型的整数;
你可以查看帮助文档 Integer 类,它有com ...

哦,这样啊,那他的问题是出在那了那
回复 使用道具 举报
张强1 发表于 2013-7-22 10:19
哦,这样啊,那他的问题是出在那了那

是因为string类无法转换成强制转换成int类吗?

点评

嗯。  发表于 2013-7-22 10:33
回复 使用道具 举报
崔一恒 发表于 2013-7-21 22:03
你应该没运行吧  int num = s1.getScores().compareTo(s2.getScores());是这句出的问题

我没有运行。没细看代码。
  int num = s1.getScores().compareTo(s2.getScores());//这句肯定也是有问题的,int型不能调用compareTo方法。应该用Integer包装一下。
还有我说的那个问题也是存在的。 Integer构造函数传入非数字的字符串作为参数不合适吧,我想应该会报错
回复 使用道具 举报
哦哦 谢谢大家
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马