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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈雨 中级黑马   /  2013-4-12 20:20  /  1842 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陈雨 于 2013-4-13 14:04 编辑

学完泛型,正好有一道基础测试题目,拿来练手,按成绩排序不行,但按姓名排序打印成绩结果又可以.
//定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性,按成绩排序,用TreeSet和Comparator实现
import java.util.*;
class Test10
{
        public static void main(String[] args)
        {
        TreeSet<Student> ts=new TreeSet<Student>(new Stucom());
        ts.add(new Student("Jack",19,80));
        ts.add(new Student("Tom",20,90));
        ts.add(new Student("Lili",18,70));
        ts.add(new Student("Lucy",21,85));
        ts.add(new Student("Hobe",19,75));
        
        Iterator<Student> it = ts.iterator();
               
                while(it.hasNext())
                {
                        System.out.println(it.next().getScore());
               
                }
        }
}

class Stucom implements Comparator<Student>
{
        public int compare(Student s1,Student s2)
        {
        if(!(s1 instanceof Student))
                throw new RuntimeException("不是学生对象");
        if(!(s2 instanceof Student))
                throw new RuntimeException("不是学生对象");
        
        return s1.getScore().compareTo(s2.getScore ());

//这里如果换成getName,代码打印出来没问题,搞不懂为什么换成成绩就不行了.
               
        }
}

class Student
{
        private String name;
        private int age;
        private int score;
        Student(String name,int age,int score)
        {
        this.name=name;
        this.age=age;
        this.score=score;
        }
        public void setShuXing(String n,int b,int c)//对私有变量设置值。
        {
                this.name=name;
                this.age=age;
                this.score=score;
        }
        public String getName( )//私有变量获取值。
        {
                return name;
        }
        
        public int  getAge( )
        {
                return age;
        }
        public int getScore( )
        {
                return score;
        }
}

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

4 个回复

倒序浏览
本帖最后由 张洪慊 于 2013-4-12 20:47 编辑
  1. class Stucom implements Comparator<Student>
  2. {
  3.         public int compare(Student s1,Student s2)
  4.         {
  5.         if(!(s1 instanceof Student))
  6.                 throw new RuntimeException("不是学生对象");
  7.         if(!(s2 instanceof Student))
  8.                 throw new RuntimeException("不是学生对象");
  9.         
  10.         return s1.getScore().compareTo(s2.getScore ());//getScore()返回int,在这里不会自动装箱->int类型不能调用方法,可以通过对象->要想调用Integer的compareTo->手动装箱
  11.        ->即 return new Integer(s1.getScore()).compareTo(new Integer(s2.getScore()));

  12.                
  13.         }
  14. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

回复 使用道具 举报
这是因为你的方法错了,int型没有compareTo方法,你应该把它转换成integer类型再调用compareTo方法。
这句 return s1.getScore().compareTo(s2.getScore ());应该改写成:return new Integer(s1.getScore()).compareTo(new Integer(s2.getScore ()));

这是包装类里面的内容。你可以看看API文档java.lang包下的各种类型,他们提供了一些基本数据类型没有的方法,必要的时候需要转换成他们。
回复 使用道具 举报
若在取出打印时,加上这些就完美了:
                        Student stu = it.next();       
                        System.out.println(stu.getName()+"---"+stu.getAge()+"---"+stu.getScore()+"---");

                //按成绩排序     
                int num = new Integer( s1.getScore()).compareTo(new Integer(s2.getScore()));
                         if (num == 0){
                        //如果学生的成绩相同,则再以姓名排序
                        return s1.getName().compareTo(s2.getName());
                }
                return num;
                }

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
如果问题未解决,请继续追问,如果没有问题了,请将帖子分类 改为“已解决”,谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马