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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hyace 中级黑马   /  2014-3-21 23:43  /  4291 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


最近几天开始申请的步骤,之前一直用的是C/C++,Java只是基础的掌握而已,写出来的代码不一定够规范正确,所以我想发到这里请大家参详,希望能够指正,谢谢~
10、 声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)。
  1. package com.itheima;
  2. /**
  3. * 10、 声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)。
  4. * @author Hyace
  5. *
  6. */
  7. import java.util.*;

  8. //定义一个Student类
  9. class Student implements Comparable{
  10.         String name;
  11.         int age;
  12.         int score;
  13.         //构造方法
  14.         Student(String name,int age,int score){
  15.             this.name=name;
  16.             this.age=age;
  17.             this.score=score;
  18.         }
  19.         //按顺序输出时的比较方法
  20.         public int compareTo(Object o){
  21.             Student stu = (Student) o;
  22.             //先按照分数比较,分数相同按照名字
  23.             if(this.score>stu.score) return 1;
  24.             else if(this.score<stu.score) return -1;
  25.             else if(this.name.compareTo(stu.name)>0) return 1;
  26.             else if(this.name.compareTo(stu.name)<0)return -1;
  27.             return 0;
  28.         }
  29.         //重写toString方法
  30.         public String toString(){
  31.             return this.name+'\0'+this.age+'\0'+this.score;
  32.         }
  33.     }

  34. public class Test10 {

  35.     public static void main(String[] args){
  36.         //声明TreeSet
  37.         TreeSet tr = new TreeSet();
  38.         tr.add(new Student("A",21,89));
  39.         tr.add(new Student("B",27,88));
  40.         tr.add(new Student("C",23,95));
  41.         tr.add(new Student("D",20,84));
  42.         tr.add(new Student("E",25,88));
  43.         System.out.println(tr);
  44.         
  45.     }

  46. }
复制代码



评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

11 个回复

倒序浏览
可以这样,个人观点。。。

public int compareTo(Object o){

            Student stu = (Student) o;

看上去肯定传来的是 Student。。但是为了规范 在之前写上
if(o intanceof Student){   //判断这个Object o是不是Student的类型。 - - 虽然很没必要
   Student stu=(Student)o;
}

然后在下面的比较部分写的有点麻烦,因为JAVA本身就有一个包装类Integer 里面自带ComperTo方法

return new Integer(this.score).compareTo(new Integer(stu.score)        这样就可以了,大就返回大于0 ,小就返回小于0 等于 返回0  你可以去看下API
然后下面也没你这么写的吧 。。(我是没见过)
这样比较好 把上面的改一下
int a=new Integer(this.score).compareTo(new Integer(stu.score) ;
if(a==0){  //意思是相等
   return  this.name.compareTo(stu.name)    //因为name 本身就是String包装类,里面有compareTo方法所以可以不像上面成绩那样 要new Integer.
}else{
return a;
}

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
Teale 发表于 2014-3-23 21:26
可以这样,个人观点。。。

public int compareTo(Object o){

谢谢同学~
真的帮助很大,对java的了解差不多超不过和C/C++相同的部分,所以现在正在努力学习!我挂出来的目的也达到了~
回复 使用道具 举报
hyace 发表于 2014-3-23 21:43
谢谢同学~
真的帮助很大,对java的了解差不多超不过和C/C++相同的部分,所以现在正在努力学习!我挂出来 ...

你只是面向对象还不是用的很好。多熟悉就行
回复 使用道具 举报
参考一下
回复 使用道具 举报
厉害  比我强多了
回复 使用道具 举报
  1. package com.itheima;

  2. import java.util.TreeSet;


  3. @SuppressWarnings("rawtypes")
  4. class Student implements Comparable {
  5.         String name;
  6.         int age;
  7.         int score;

  8.         // 构造方法
  9.         Student(String name, int age, int score) {
  10.                 this.name = name;
  11.                 this.age = age;
  12.                 this.score = score;
  13.         }

  14.         // 按顺序输出时的比较方法
  15.         public int compareTo(Object o) {
  16.                 Student stu = (Student) o;
  17.         // 先按照分数比较,分数相同按照名字
  18.                 if (this.score > stu.score)
  19.                         return 1;
  20.                 else if (this.score < stu.score)
  21.                         return -1;
  22.                 else if (this.name.compareTo(stu.name) > 0)
  23.                         return 1;
  24.                 else if (this.name.compareTo(stu.name) < 0)
  25.                         return -1;
  26.                 return 0;
  27.         }
  28.        
  29.         @Override
  30.         //toString
  31.         public String toString() {
  32.                 return "Student [name=" + name + ", age=" + age + ", score=" + score
  33.                                 + "]";
  34.         }

  35. }

  36. public class Test10 {
  37.         public static void main(String[] args) {
  38.                 //创建集合对象
  39.                 TreeSet<Student> treeSet = new TreeSet<>();
  40.                 //添加元素
  41.                 treeSet.add(new Student("A", 18, 96));
  42.                 treeSet.add(new Student("B", 20, 95));
  43.                 treeSet.add(new Student("D", 20, 95));
  44.                 treeSet.add(new Student("C", 21, 88));
  45.                
  46.                 System.out.println(treeSet);

  47.         }
  48. }
复制代码
回复 使用道具 举报
谁能回答我下,像集合里面添加元素的时候是怎么替换对象的啊。这部分的代码在哪里看??
回复 使用道具 举报
学习了,对于基础不是很强的我来说,很受用!
回复 使用道具 举报
写的很详细,学习了
回复 使用道具 举报
Teale 发表于 2014-3-23 21:26
可以这样,个人观点。。。

public int compareTo(Object o){

这样也行不过不像上面的好理解!
回复 使用道具 举报
如果名字也相同那么还要比较 年龄吗?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马