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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© msyx9871453 中级黑马   /  2015-3-11 11:15  /  1279 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)。
我的基础第十题,怎么也弄不明白了,希望会的大神帮帮忙。谢谢
public class Test10 {

        public static void main(String[] args) {
               
                TreeSet<Student> treeSet = new TreeSet<Student>();
                Student s1 = new Student("s1", 20, 80);
                Student s2 = new Student("s2", 20, 80);
                Student s3 = new Student("s3", 20, 90);
                Student s4 = new Student("s4", 20, 70);
                Student s5 = new Student("s5", 20, 60);
               
                treeSet.add(s1);
                treeSet.add(s2);
                treeSet.add(s3);
                treeSet.add(s4);
                treeSet.add(s5);
                /*
                Iterator iterator = treeSet.iterator();
                while(iterator.hasNext()){
                        Student student = (Student) iterator.next();
                        System.out.println(student.getScore());
                }*/
               
                for(Student student : treeSet){
                        System.out.println(student.getScore());
                }
        }
}

13 个回复

倒序浏览
这个是我当时做得代码
  1. /*
  2. * 10、声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)
  3. */
  4. package com.itheima;

  5. import java.util.*;

  6. public class Test10 {
  7.         public static void main(String[] args){
  8.                 TreeSet ts=new TreeSet();
  9.                 ts.add(new Student("张三",23,90));
  10.                 ts.add(new Student("李四",23,80));
  11.                 ts.add(new Student("王五",22,80));
  12.                 ts.add(new Student("赵六",22,80));
  13.                 ts.add(new Student("钱七",22,80));
  14.                 System.out.println(ts);
  15.         }
  16. }

  17. /*
  18. * 学生类
  19. */
  20. class Student implements Comparable{
  21.         private String name;
  22.         private int age;
  23.         private double score;
  24.        
  25.         public String getName(){
  26.                 return name;
  27.          }
  28.          public int getAge(){
  29.                  return age;
  30.          }
  31.          public double getscore(){
  32.                  return score;
  33.          }
  34.          
  35.          
  36.         Student(String name,int age,double score){
  37.                 this.name=name;
  38.                 this.age=age;
  39.                 this.score=score;
  40.          }
  41.         @Override
  42.         public int compareTo(Object obj) {
  43.                 if (!(obj instanceof Student))
  44.                         throw new RuntimeException("不是学生类型");
  45.                         Student s = (Student) obj;
  46. //                        按成绩排序,成绩一样按名字
  47.                         if (this.score >s.score)return -1;
  48.                         else if(this.score<s.score)return 1;
  49.             else if(this.name.compareTo(s.name)>0) return 1;
  50.             else if(this.name.compareTo(s.name)<0)return -1;

  51.                         return 0;
  52.                
  53.         }
  54.         public String toString(){
  55.                 return "姓名:"+this.name+"成绩:"+this.score+"年龄:"+this.age;
  56.         }
  57. }
  58.                
  59.        
复制代码


评分

参与人数 1技术分 +1 收起 理由
万合天宜 + 1 老毕的视频讲的很详细~

查看全部评分

回复 使用道具 举报
谢谢了。
回复 使用道具 举报
思路:实现comparable接口,覆盖public int compareTo(Object obj) 方法,在该方法中定义自己的排序规则。
回复 使用道具 举报
这个很简单,这道题主要还是考TreeSet集合的特性:ThreeSet集合的数据结构是二叉数结构,在排列前,你必须使学生对象具有比较性,去实现 comparer接口,然后在重写compareTo方法,以学生成绩为判断依据
回复 使用道具 举报 1 0
考的这么难啊,表示看不懂
回复 使用道具 举报
基础题真心难  我现在看到接口 才会做两题
回复 使用道具 举报
因为题目里面说了要考虑成绩相同的问题,所以最好是用比较类来做
回复 使用道具 举报
小菜鸟表示看不懂!
回复 使用道具 举报
题目还能叫别人帮忙?
回复 使用道具 举报
表示没看懂,老老实实看视频
回复 使用道具 举报
不用着急啊,有没有时间限制的。
回复 使用道具 举报
太阳 发表于 2015-3-11 17:27
考的这么难啊,表示看不懂

有简单的 这是最后一道,别灰心
回复 使用道具 举报
这个题目和我上次的一样
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马