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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 袁錦泰 黑马帝   /  2012-5-16 19:11  /  2955 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 袁錦泰 于 2012-5-16 21:18 编辑
  1. import java.util.Iterator;
  2. import java.util.TreeSet;

  3. public class Student implements Comparable<Object> {

  4.         private String name;
  5.         private int age;
  6.         private double score;

  7.         Student(String name, int age, double score) {

  8.                 this.name = name;
  9.                 this.age = age;
  10.                 this.score = score;
  11.         }

  12.         public void setName(String name) {
  13.                 this.name = name;
  14.         }

  15.         public String getName() {
  16.                 return name;
  17.         }

  18.         public void setAge(int age) {
  19.                 this.age = age;
  20.         }

  21.         public int getAge() {
  22.                 return age;
  23.         }

  24.         public void setScore(double score) {
  25.                 this.score = score;
  26.         }

  27.         public double getScore() {
  28.                 return score;
  29.         }

  30.         public static void main(String[] args) {

  31.                 TreeSet<Student> ts = new TreeSet<Student>();
  32.                 ts.add(new Student("zhangsan", 18, 87));
  33.                 ts.add(new Student("lisi", 18, 76));
  34.                 ts.add(new Student("wangwu", 19, 65));
  35.                 ts.add(new Student("zhaoliu", 17, 69));
  36.                 ts.add(new Student("zhouqi", 19, 92));
  37.                 for (Iterator<Student> it = ts.iterator(); it.hasNext();) {
  38.                         Student stu = (Student) it.next();
  39.                         System.out.println("姓名:" + stu.getName() + "年齡:" + stu.getAge()
  40.                                         + "成績" + stu.getScore());
  41.                 }
  42.         }

  43.         public int compareTo(Object obj) {
  44.         /*
  45.         我记得在数据类型进行强转时需要健壮性判断
  46.         我写的是if( obj instanceof Student),因为需要返回值,我突然不知道该怎么写了
  47.         有谁能帮我分析一下,确定一下if(obj instanceof Student){}的范围
  48.        */
  49.                 Student stu = (Student) obj;
  50.                 // int temp = (int)(this.score-stu.score);
  51.                 // return temp==0?this.name.compareTo(stu.name):score;
  52.                 if (stu.score > this.score) {
  53.                         return 1;
  54.                 }
  55.                 if (stu.score < this.score) {
  56.                         return -1;
  57.                 } else {
  58.                         return stu.name.compareTo(this.name);
  59.                 }
  60.         }
  61. }
复制代码

7 个回复

正序浏览
袁錦泰 黑马帝 2012-5-17 15:17:06
8#
李文富 发表于 2012-5-17 13:54
嗯,我这是在基础测试时写的,也考虑到了score是小数,给你参考下
----------------
/**

谢谢你!
回复 使用道具 举报
嗯,我这是在基础测试时写的,也考虑到了score是小数,给你参考下
----------------
/**
5、定义一个学生类, 需要有
姓名, 年龄, 考试成绩三个成员属性.
属性(成员变量)需要私有并提供get, set方法,
可以通过构造函数进行初始化。
*/

public class StudentDemo
{
        public static void main(String[] args)
        {
                Student stu = new Student("harry", 15, 88.5) ; //构造一个学生对象;
                System.out.println(stu.getName() + "  " + stu.getAge() + "  " +stu.getScore());
                stu.setName("cobe");//设置 name ,age ,score;
                stu.setAge(26);
                stu.setScore(89.5);
                System.out.println(stu.getName() + "  " + stu.getAge() + "  " +stu.getScore());
        }
}

//将编译得到得Stduent.class和SortMyStuDemo类放同一目录一起编译;
class Student
{
        public Student(String name, int age, double score)
        {
                this.name = name ;
                this.age = age ;
                this.score = score;
        }

        public String getName()//获取姓名
        {
                return name;
        }
       
        public int getAge()//获取年龄
        {
                return age;
        }

        public double getScore()//获取分数
        {
                return score;
        }

        public void setName(String name)//设置姓名
        {
                this.name = name;
        }

        public void setAge(int age)//设置年龄
        {
                this.age = age;
        }

        public void setScore(double score)//设置分数
        {
                this.score = score;
        }

private String name;
private int age;
private double score;
}


----------------------------
import java.util.*;

public class SortMyStuDemo
{
        public static void main(String[] args)
        {
                TreeSet<Student> ts = new TreeSet<Student>(new MyCompare());//创建树集,使用MyCompare类排序
                ts.add(new Student("Harry" , 14, 87  ));
                ts.add(new Student("Ameily", 12, 87.5));
                ts.add(new Student("Cobber", 13, 85  ));
                ts.add(new Student("Obamar", 17, 84.5));
                ts.add(new Student("James" , 19, 86.5));

                Iterator<Student> it = ts.iterator();
                while(it.hasNext())
                {   Student stu = it.next();
                        System.out.println("name="+stu.getName()+"... age="+ stu.getAge()+
                                "...score="+stu.getScore());
                }
               
        }
}

class MyCompare implements Comparator<Student>
{
        public int compare(Student s1, Student s2)
        {
                double num = s1.getScore() - s2.getScore();
                if ( num == 0)    //分数相同则按照姓名排序
                                return s1.getName().compareTo(s2.getName());
                else if (num > 0) //使用分数的升序排序;
                        return 1;
                return -1;
        }
}

/*
不使用comparTo方法直接比较分数;而是利用了compareTo方法的返回值就可比较分数大小,从而返回treeset排序的依据;
*/

评分

参与人数 1技术分 +1 收起 理由
职业规划-刘倩老师 + 1 赞一个!

查看全部评分

回复 使用道具 举报
袁錦泰 发表于 2012-5-16 21:37
我刚脑子转过来了,我加了个!就不用在为判断语句后面的{}犯愁了,你刚说加泛型就不需要这样写了?能不能 ...

理解错了,不是针对这个的
回复 使用道具 举报
高云飞 发表于 2012-5-16 21:08
if (!(obj instanceof Student)) {
}
其实,用到泛型以后,就不用这样写了。

我刚脑子转过来了,我加了个!就不用在为判断语句后面的{}犯愁了,你刚说加泛型就不需要这样写了?能不能再说得具体一点。
回复 使用道具 举报
if (!(obj instanceof Student)) {
}
其实,用到泛型以后,就不用这样写了。
回复 使用道具 举报
隋丙跃 发表于 2012-5-16 21:00
  • public int compareTo(Object obj) {        
  •                 Student stu = (Student) obj;

  • 我开始也是这样写的,但因为score(成绩)的数据类型是double,我没有想出该如何才能使用你的这种简写格式。我想弄明白在强转之前做出健壮性判断该怎么写,尤其是我如果写if(obj instanceof Student){},我的{}的范围是什么,因为有返回值需要写return语句。不知说的是否清楚,还是麻烦跃哥帮忙瞧瞧。
    回复 使用道具 举报
    • public int compareTo(Object obj) {        
    •                 Student stu = (Student) obj;
    •                  int temp = (int)(this.score-stu.score);
    •                return temp==0?this.name.compareTo(stu.name):temp;
    • }
    哦了
    不过我没测试啊
    回复 使用道具 举报
    您需要登录后才可以回帖 登录 | 加入黑马