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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杜光 高级黑马   /  2013-6-9 18:54  /  1374 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杜光 于 2013-6-11 09:47 编辑

这个代码中 我不是很明白哪段把输入的数据给排序了?

  1. /*
  2. 有五个学生,每个学生有3们课的成绩,
  3. 从键盘输入以上数据(包括姓名,三门课成绩)
  4. 输入的格式:如:zhangsan,30,40,60计算出总成绩。
  5. 并把学生的信息和计算出的总分数高低顺序存放在磁盘文件“stud.txt”中。

  6. 1.描述学生对象。
  7. 2.定义一个可操作的学生对象的工具类。

  8. 思想:
  9. 1.通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
  10. 2.因为学生有很多,那么就需要存储,使用到集合。因为要对学生的总分排序。所以可以使用TreeSet。
  11. 3.将集合的信息写入到一个文件中。

  12. */


  13. import java.util.*;
  14. import java.io.*;

  15. class Student implements Comparable<Student>
  16. {
  17.         private String name;
  18.         private int ma,cn,en;
  19.         private int sum;

  20.         Student(String name,int ma,int cn,int en)
  21.         {
  22.                 this.name = name;
  23.                 this.ma = ma;
  24.                 this.cn = cn;
  25.                 this.en = en;
  26.                 sum = ma + cn + en;
  27.         }

  28.         public int compareTo(Student s)
  29.         {
  30.                 int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  31.                 if(num==0)
  32.                         return this.name.compareTo(s.name);
  33.                 return num;
  34.         }

  35.         public String getName()
  36.         {
  37.                 return name;
  38.         }
  39.         
  40.         public int getSum()
  41.         {
  42.                 return sum;
  43.         }

  44.         public int hashCode()
  45.         {
  46.                 return name.hashCode()+sum*78;
  47.         }

  48.         public boolean equals(Object obj)
  49.         {
  50.                 if(!(obj instanceof Student))
  51.                 {
  52.                         throw new ClassCastException("类型不匹配");
  53.                 }
  54.                 Student s = (Student)obj;

  55.                 return this.name.equals(s.name) && this.sum==s.sum;
  56.         }

  57.         public String toString()
  58.         {
  59.                 return "Student["+name+","+ma+","+cn+","+en+"]";
  60.         }

  61. }

  62. class StudentInfoTool
  63. {
  64.         public static Set<Student> getStudents()throws IOException
  65.         {
  66.                 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

  67.                 String line = null;

  68.                 Set<Student> stus = new TreeSet<Student>();

  69.                 while((line=bufr.readLine())!=null)
  70.                 {
  71.                         if("over".equals(line))
  72.                                 break;
  73.                         
  74.                         String[] info = line.split(",");

  75.                         Student stu = new Student(info[0],Integer.parseInt(info[1]),
  76.                                                                                           Integer.parseInt(info[2]),
  77.                                                                                           Integer.parseInt(info[3]));
  78.                         stus.add(stu);        
  79.                 }

  80.                 bufr.close();

  81.                 return stus;

  82.         }

  83.         public static void write2File(Set<Student> stus)throws IOException
  84.         {
  85.                 BufferedWriter bufw = new BufferedWriter(new FileWriter("stuinfo.txt"));

  86.                 for(Student stu : stus)
  87.                 {
  88.                         bufw.write(stu.toString()+"\t");
  89.                         bufw.write(stu.getSum()+"");
  90.                         bufw.newLine();
  91.                         bufw.flush();
  92.                 }

  93.                 bufw.close();
  94.         }
  95. }




  96. class  StudentInfoTest
  97. {
  98.         public static void main(String[] args) throws IOException
  99.         {
  100.                 Set<Student> stus = StudentInfoTool.getStudents();

  101.                 StudentInfoTool.write2File(stus);
  102.                
  103.         }
  104. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

4 个回复

倒序浏览
明确一下  就是  他这里面排序  你用的是集合类的特点
集合里面  TreeMap和 TreeSet底层都是二叉树结构  只要 你的实体类Student 实现了Comparable接口 或者在实例化这两种集合中的任意一种集合实例的时候
传入Comparator接口实现类的实例  都可以在TreeSet add元素的时候  自动按照底层二叉树排好序(排序的规则就是上面说的)
添加完元素的时候  你的TreeSet也就把元素排序完成了
这也体现了  你传入的顺序是一种 但是 输出的顺序又是一种  两种顺序不同
体现了Set集合的无序性

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
TreeSet集合中的元素是有序排放的,其排序依据是元素的conpareTo方法.也就是在添加元素的时候,函数会底层调用此方法
  1.         public int compareTo(Student s)
  2.         {
  3.                 int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  4.                 if(num==0)
  5.                         return this.name.compareTo(s.name);
  6.                 return num;
  7.         }
复制代码
先按照总分排序,如果总分相等,再按照姓名排序

建议楼主可以看看treeSet的排序原理

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
Student类实现了Comparable接口的compareTo方法,该方法用于比较排序
Integer类和String类都实现了相对应的compareTo方法,所以代码中直接调用 了
  1.         public int compareTo(Student s)
  2.         {
  3.                 int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  4.                 if(num==0)
  5.                         return this.name.compareTo(s.name);
  6.                 return num;
  7.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马