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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 遮天 中级黑马   /  2014-4-3 10:17  /  3000 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 遮天 于 2014-4-7 09:52 编辑

       这是我基础测试题中的一道题目:声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet, 按照成绩排序输出结果(考虑成绩相同的问题)。

下面是我做的答案
  1. /**
  2. * 第10题:声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,
  3. * 按照成绩排序输出结果(考虑成绩相同的问题)。
  4. *
  5. *
  6. *
  7. *
  8. *
  9. *
  10. * */
  11. import java.util.*;
  12. public class Test10
  13. {

  14. public static void main(String[] args)
  15. {
  16. TreeSet<Student> ts=new TreeSet<Student>();

  17. ts.add(new Student("程序员06",23,89.2));
  18. ts.add(new Student("程序员10",45,96.0));
  19. ts.add(new Student("程序员08",32,98.5));
  20. ts.add(new Student("程序员02",34,96.0)); //成绩相同
  21. ts.add(new Student("程序员05",14,95.5));

  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()+"******"+stu.getscore());
  27. }

  28. }

  29. }

  30. class Student implements Comparable //该接口强制让学生具备比较性
  31. <Object>
  32. {
  33. private String name;
  34. private int age;
  35. private double score;

  36. Student(String name,int age,double score)
  37. {
  38. this.name=name;
  39. this.age=age;
  40. this.score=score;
  41. }

  42. public int compareTo(Object obj)
  43. {
  44. if(!(obj instanceof Student))
  45. throw new RuntimeException("不是学生类型");
  46. Student s=(Student)obj;

  47. if(this.score<s.score)
  48. return 1;
  49. if(this.score==s.score)
  50. {
  51. //成绩相同时比较姓名
  52. return this.age.compareTo(s.age);
  53. }
  54. return -1;
  55. }

  56. public String getName()
  57. {
  58. return name;
  59. }

  60. public int getAge()
  61. {
  62. return age;
  63. }

  64. public double getscore()
  65. {
  66. return score;
  67. }
  68. }
复制代码


本来是想要在出现成绩相同的情况下,比较年龄
  1. if(this.score==s.score)
  2. {
  3. //成绩相同时比较年龄
  4. return this.age.compareTo(s.age);
  5. }
复制代码

结果提示就报错了,那么代码如何写能比较年龄呢?为什么?


cw.jpg (32.55 KB, 下载次数: 29)

成绩相同时比较年龄的错误提示

成绩相同时比较年龄的错误提示

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

9 个回复

倒序浏览
this.age.compareTo(s.age); 改为:return new Integer(this.age).compareTo(new Integer(s.age));   compareTo只适合实现了Comparable的对象类型

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 李军辉 于 2014-4-3 10:48 编辑


if(this.score<s.score)
return 1;
if(this.score==s.score)
{
//成绩相同时比较姓名
return (this.age-s.age);//相减
}
return -1;
}
回复 使用道具 举报
compareTo用于比较引用数据类型,你的age是int型,直接用> == <,比较就好了
或者把age定义成对象也可以
回复 使用道具 举报
return this.age.compareTo(s.age);

修改为 return this.age-s.age;
回复 使用道具 举报
你的代码:
if(this.score==s.score)
{
//成绩相同时比较年龄
return this.age.compareTo(s.age);
}
报错的原因是你的age定义的是int类型的,注意基本数据类型是没有compareTo()方法的
最简单的修改,把你的age改为int的包装类Integer类型

只需要改这一行代码:
private int age;
改为private Integer age;

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
==整数是用==
回复 使用道具 举报
  1. package cn.com.problem;

  2. import java.util.*;

  3. public class ggg {

  4.         public static void main(String[] args) {
  5.                 TreeSet<Student> ts = new TreeSet<Student>();

  6.                 ts.add(new Student("程序员06", 23, 89.2));
  7.                 ts.add(new Student("程序员10", 45, 96.0));
  8.                 ts.add(new Student("程序员08", 32, 98.5));
  9.                 ts.add(new Student("程序员02", 34, 96.0)); // 成绩相同
  10.                 ts.add(new Student("程序员05", 14, 95.5));

  11.                 Iterator<Student> it = ts.iterator();
  12.                 while (it.hasNext()) {
  13.                         Student stu = (Student) it.next();
  14.                         System.out.println(stu.getName() + "******" + stu.getAge()
  15.                                         + "******" + stu.getscore());
  16.                 }

  17.         }

  18. }

  19. class Student implements Comparable // 该接口强制让学生具备比较性
  20.                 <Object> {
  21.         private String name;
  22.         private int age;
  23.         private double score;

  24.         Student(String name, int age, double score) {
  25.                 this.name = name;
  26.                 this.age = age;
  27.                 this.score = score;
  28.         }

  29.         public int compareTo(Object obj) {
  30.                 if (!(obj instanceof Student))
  31.                         throw new RuntimeException("不是学生类型");
  32.                 Student s = (Student) obj;

  33.                 if (this.score < s.score)
  34.                         return 1;
  35.                 if (this.score == s.score) {
  36.                         // 成绩相同时比较姓名
  37. //                        return this.age.compareTo(s.age);
  38.                         //Cannot invoke compareTo(int) on the primitive type int这是在将鼠标放在上面那句话时,出现的错误。
  39.                         //你好好跟一下就知道问题所在了。invoke是调用方法的意思。错误的原因是:不能再基础类型int上调用compareTo方法与基本类型int比较
  40.                         //所以解决发放很明显,你int强制转换成一个不是int类型的就可以了,比如说Integer、String都行
  41.                         //        return new Integer(this.age).compareTo(new Integer(s.age));//这种写法也可以
  42.                         return new String(String.valueOf(this.age)).compareTo(String.valueOf(s.age));//百度一下,就能看见compareTo用于字符串的字符顺序比较。
  43.                         //所以将其换成字符串
  44.                 }
  45.                 return -1;
  46.         }

  47.         public String getName() {
  48.                 return name;
  49.         }

  50.         public int getAge() {
  51.                 return age;
  52.         }

  53.         public double getscore() {
  54.                 return score;
  55.         }
  56. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
comperTo只能比较一些对象,而int和Integer区别恰好就在这里
回复 使用道具 举报
把int型包装成Integer型,还有加了泛型,迭代就不用强转了,看了不自在
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马