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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 罗全涛 黑马帝   /  2011-12-29 20:15  /  1953 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 罗全涛 于 2011-12-30 10:48 编辑

  1. /*

  2. 有五个学生,每个学生有三门课的成绩
  3. 从键盘输入以上数据(包括姓名,三门课的成绩)
  4. 输入的格式,如:zhangsan,30,40,50计算出总成绩
  5. 并把学生的信息和计算出的总分数高低顺序放到磁盘文件“studentinfo.txt”中
  6. */
  7. import java.io.*;
  8. import java.util.*;
  9. class Student //implements Comparable<Student> //1:既然后面用到TreeSet比较此处实现Comparable接口是不是有点不需要:2:但是注释起来后,在getStudent函数中  stus.add(stu);          上报出类型不匹配,说是不能讲类型匹配到Comparable上,可是明明TreeSet的add方法中并没有说添加进去的元素对象的类一定要实现comparable啊?不太懂希望明白的帮忙分析一下吧
  10. {
  11.         private String name;
  12.         private int  ma,cn,en;
  13.         private int sum;
  14.         Student(String name, int ma, int cn, int en)
  15.         {
  16.                 this.name = name;
  17.                 this.ma = ma;
  18.                 this.cn = cn;
  19.                 this.en = en;
  20.                 sum = ma + cn +en;
  21.         }
  22.         public int compareTo(Student s)
  23.         {
  24.                 int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  25.                 if (num==0)
  26.                         return this.name.compareTo(s.name);
  27.                 return num;
  28.         }
  29.         public String getName()
  30.         {
  31.                 return name;
  32.         }
  33.         public int getSum()
  34.         {
  35.                 return sum;
  36.         }
  37.         public int hashCode()
  38.         {
  39.                 return name.hashCode()+sum*78;
  40.         }
  41.         public boolean equals(Object obj)
  42.         {
  43.                 if(!(obj instanceof Student))
  44.                         throw new ClassCastException("类型不匹配");
  45.                 Student s = (Student)obj;
  46.                 return this.name.equals(s.name) && this.sum==s.sum;
  47.         }
  48.         public String toString()
  49.         {
  50.                 return "student["+name+","+ma+","+cn+","+en+"]";
  51.         }
  52. }
  53. class StduentInfoTool
  54. {
  55.         public static Set<Student> getStudents() throws IOException
  56.         {
  57.                 return getStudents(null);
  58.         }
  59.         public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException
  60.         {
  61.                 BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
  62.                 String line = null;
  63.                 Set<Student> stus=null;
  64.                 if (cmp ==null)
  65.                         stus= new TreeSet<Student>();
  66.                 else
  67.                         stus= new TreeSet<Student>(cmp);
  68.                 while((line = bfr.readLine())!=null) //只有BufferedReader 有ReadLine()方法,当返回为null,表示文件读到末尾
  69.                 {
  70.                         if("over".equals(line))
  71.                                 break;
  72.                         String[] info = line.split(",");
  73.                         Student stu  = new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
  74.                         stus.add(stu);               
  75.                 }
  76.                 bfr.close();
  77.                 return stus;
  78.         }
  79.         public static void write2File(Set<Student> stus) throws IOException
  80.         {
  81.                 BufferedWriter bfw = new BufferedWriter(new FileWriter("studentinfo.txt"));
  82.                 for(Student stu : stus)
  83.                 {
  84.                         bfw.write(stu.toString()+"\t");
  85.                         bfw.write(stu.getSum()+"");
  86.                         bfw.newLine();
  87.                 }
  88.                 bfw.close();
  89.         }

  90. }
  91. class  StudentInfoTest
  92. {
  93.         public static void main(String[] args)  throws IOException
  94.         {
  95.                 Comparator<Student> cmp=Collections.reverseOrder();
  96.                 Set<Student> stus = StduentInfoTool.getStudents(cmp);
  97.                 StduentInfoTool.write2File(stus);
  98.         }
  99. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
吴上储 + 1

查看全部评分

4 个回复

倒序浏览
本帖最后由 罗全涛 于 2011-12-30 09:21 编辑

大家帮忙看看吧?说说自己的见解也行啊
回复 使用道具 举报
TreeSet能够进行排序的前提:元素具有比较性或者TreeSet集合自身具有比较性。
--元素Student具有比较性的方法:实现Comparable接口,重写compareTo()方法
--TreeSet集合自身具有比较性的方法:定义一个比较器(实现Comparator接口,重写compare()方法),作为参数传递给TreeSet集合的构造函数

这边修改成定义比较器实现排序:
                Comparator<Student> cmp=Collections.reverseOrder(
               
                new Comparator<Student>(){
                        public int compare(Student a,Student b)
                        {
                                int num=new Integer(a.getSum()).compareTo(new Integer(b.getSum()));
                                if(num==0)
                                        return a.getName().compareTo(b.getName());
                                return num;
                        }
                        }
               
                );
  1. /*

  2. 有五个学生,每个学生有三门课的成绩
  3. 从键盘输入以上数据(包括姓名,三门课的成绩)
  4. 输入的格式,如:zhangsan,30,40,50计算出总成绩
  5. 并把学生的信息和计算出的总分数高低顺序放到磁盘文件“studentinfo.txt”中
  6. */
  7. import java.io.*;
  8. import java.util.*;
  9. class Student //implements Comparable<Student> //1:既然后面用到TreeSet比较此处实现Comparable接口是不是有点不需要:2:但是注释起来后,在getStudent函数中  stus.add(stu);          上报出类型不匹配,说是不能讲类型匹配到Comparable上,可是明明TreeSet的add方法中并没有说添加进去的元素对象的类一定要实现comparable啊?不太懂希望明白的帮忙分析一下吧
  10. {
  11.         private String name;
  12.         private int  ma,cn,en;
  13.         private int sum;
  14.         Student(String name, int ma, int cn, int en)
  15.         {
  16.                 this.name = name;
  17.                 this.ma = ma;
  18.                 this.cn = cn;
  19.                 this.en = en;
  20.                 sum = ma + cn +en;
  21.         }
  22.         public int compareTo(Student s)
  23.         {
  24.                 int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  25.                 if (num==0)
  26.                         return this.name.compareTo(s.name);
  27.                 return num;
  28.         }
  29.         public String getName()
  30.         {
  31.                 return name;
  32.         }
  33.         public int getSum()
  34.         {
  35.                 return sum;
  36.         }
  37.         public int hashCode()
  38.         {
  39.                 return name.hashCode()+sum*78;
  40.         }
  41.         public boolean equals(Object obj)
  42.         {
  43.                 if(!(obj instanceof Student))
  44.                         throw new ClassCastException("类型不匹配");
  45.                 Student s = (Student)obj;
  46.                 return this.name.equals(s.name) && this.sum==s.sum;
  47.         }
  48.         public String toString()
  49.         {
  50.                 return "student["+name+","+ma+","+cn+","+en+"]";
  51.         }
  52. }
  53. class StduentInfoTool
  54. {
  55.         public static Set<Student> getStudents() throws IOException
  56.         {
  57.                 return getStudents(null);
  58.         }
  59.         public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException
  60.         {
  61.                 BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
  62.                 String line = null;
  63.                 Set<Student> stus=null;
  64.                 if (cmp ==null)
  65.                         stus= new TreeSet<Student>();
  66.                 else
  67.                         stus= new TreeSet<Student>(cmp);
  68.                 while((line = bfr.readLine())!=null) //只有BufferedReader 有ReadLine()方法,当返回为null,表示文件读到末尾
  69.                 {
  70.                         if("over".equals(line))
  71.                                 break;
  72.                         String[] info = line.split(",");
  73.                         Student stu  = new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
  74.                         stus.add(stu);               
  75.                 }
  76.                 bfr.close();
  77.                 return stus;
  78.         }
  79.         public static void write2File(Set<Student> stus) throws IOException
  80.         {
  81.                 BufferedWriter bfw = new BufferedWriter(new FileWriter("studentinfo.txt"));
  82.                 for(Student stu : stus)
  83.                 {
  84.                         bfw.write(stu.toString()+"\t");
  85.                         bfw.write(stu.getSum()+"");
  86.                         bfw.newLine();
  87.                 }
  88.                 bfw.close();
  89.         }

  90. }
  91. class  StudentInfoTest
  92. {
  93.         public static void main(String[] args)  throws IOException
  94.         {
  95.                 //Comparator<Student> cmp=Collections.reverseOrder();
  96.                 //这边修改成定义比较器实现排序:
  97.                 Comparator<Student> cmp=Collections.reverseOrder(
  98.                
  99.                 new Comparator<Student>(){
  100.                         public int compare(Student a,Student b)
  101.                         {
  102.                                 int num=new Integer(a.getSum()).compareTo(new Integer(b.getSum()));
  103.                                 if(num==0)
  104.                                         return a.getName().compareTo(b.getName());
  105.                                 return num;
  106.                         }
  107.                         }
  108.                
  109.                 );
  110.                
  111.                 Set<Student> stus = StduentInfoTool.getStudents(cmp);
  112.                 StduentInfoTool.write2File(stus);
  113.         }
  114. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
吴上储 + 2

查看全部评分

回复 使用道具 举报
monghuan 发表于 2011-12-30 10:01
TreeSet能够进行排序的前提:元素具有比较性或者TreeSet集合自身具有比较性。
--元素Student具有比较性的方 ...

谢谢,听你这么一解释,明白是怎么回事了。
回复 使用道具 举报
不客气,相互学习,呵呵,共同进步
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马