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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李万海 中级黑马   /  2013-3-22 17:25  /  968 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李万海 于 2013-3-22 18:17 编辑
  1. import java.util.*;
  2. class  CompareToDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 TreeSet ts = new TreeSet(new MyCompare());
  7.                 ts.add(new Student("李雷",18,94));
  8.                 ts.add(new Student("韩梅",19,97));
  9.                 ts.add(new Student("露西",17,96));
  10.                 ts.add(new Student("莉莉",17,94));
  11.                 ts.add(new Student("汤姆",18,95));
  12.                 Iterator it = ts.iterator();
  13.                 while(it.hasNext())
  14.                 {
  15.                         Student stu = (Student)it.next();
  16.                         System.out.println("\t"+"姓名:"+stu.getName()+" "+"年龄:"+stu.getAge()+" "+"成绩:"+stu.getResult());
  17.                 }
  18.         }
  19. }
  20. class Student implements Comparable
  21. {
  22.         private String name;
  23.         private int age;
  24.         private int result;
  25.         Student(String name,int age,int result)
  26.         {
  27.                 this.name=name;
  28.                 this.age=age;
  29.                 this.result=result;
  30.         }
  31.         public void setName(String name)
  32.         {
  33.                 this.name=name;
  34.         }
  35.         public String getName()
  36.         {
  37.                 return name;
  38.         }
  39.         public void setAge(int age)
  40.         {        
  41.                 this.age=age;
  42.         }
  43.         public int getAge()
  44.         {
  45.                 return age;
  46.         }
  47.         public void setResult(int result)
  48.         {
  49.                 this.result=result;
  50.         }
  51.         public int getResult()
  52.         {
  53.                 return result;
  54.         }
  55. }
  56. class MyCompare implements Comparator
  57. {
  58.         public int compare(Object o1,Object o2)
  59.         {
  60.                 Student s1 =(Student)o1;
  61.                 Student s2 =(Student)o2;
  62.                 int num = new Integer(s1.getResult()).compareTo(new Integer (s2.getResult()));
  63.                 if(num==0)
  64.       return s1.getName().compareTo(s2.getName());
  65.         }
  66. }
复制代码
编译时提示没有覆盖Comparator中的方法,要覆盖方法后才能调用,应该如何覆盖?

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

2 个回复

倒序浏览
public int compare(Object o1,Object o2)
        {
                Student s1 =(Student)o1;
                Student s2 =(Student)o2;
                int num = new Integer(s1.getResult()).compareTo(new Integer (s2.getResult()));
                if(num==0)
      return s1.getName().compareTo(s2.getName());
卤煮,你的返回值没写,这一个return是写在if后面的,不能保证他一定执行,还要加上一个return num;

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
李尧 发表于 2013-3-22 18:12
public int compare(Object o1,Object o2)
        {
                Student s1 =(Student)o1;

额,谢谢了,那一句给写漏了,:lol
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马