本帖最后由 HM朱蛟 于 2013-4-27 19:13 编辑
Collections.reverseOrder())是返回一个强制反转的比较器。它具有2种形态,有参(自定义comparator)和无参(默认comparator)的。
--------------------------------------------------------------------------------------------------------------
21天的练习题代码:
从104行开始:
Comparator<Student> cmp = Collections.reverseOrder();
Set<Student> stus = StuTool.StuToolRead(cmp);
这两句为什么不能写成:
Set<Student> stus = StuTool.StuToolRead(Collections.reverseOrder());
我试了下写成这种他要报错:
C:\Documents and Settings\Administrator\桌面\BH-temp>javac fx.java
fx.java:122: 找不到符号
符号: 方法 StuToolRead(java.util.Comparator<java.lang.Object>)
位置: 类 StuTool
Set<Student> stus = StuTool.StuToolRead(Collections.reverseOrder());
^
Code:
- *import java.io.*;
- import java.util.*;
- class Student implements Comparable<Student>
- {
- private String name;
- private int en,cn,ma,sum;
- public int compareTo(Student s)
- {
- int num = new Integer(this.sum).compareTo(new Integer(s.sum));
- if(num == 0)
- return this.name.compareTo(s.name);
- return num;
- }
- public int hashCode()//err
- {
- return name.hashCode()+sum*34;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("不是学生类");
- Student s = (Student)obj;
- return this.name.equals(s.name) && this.sum == s.sum;
- }
- Student(String name,int en,int cn,int ma)
- {
- this.name = name;
- this.en = en;
- this.cn = cn;
- this.ma = ma;
- sum = en+cn+ma;
- }
- public String toString()
- {
- return "name= "+name+",en= "+en+",cn= "+cn+",ma= "+ma+";"+"\t"+"sum="+sum;
- }
- }
- class StuTool //定义该类是为了操作学生类
- {
- public static Set<Student> StuToolRead() throws IOException
- {
- return StuToolRead(null);
- }
- public static Set<Student> StuToolRead(Comparator<Student> cmp) throws IOException
- {
- /*IO操作,输入,存入*/
- //建容器存学生
- TreeSet<Student> ts = null;
- if(cmp==null)
- ts = new TreeSet<Student>();
- else
- ts = new TreeSet<Student>(cmp);
- String s =null; //读写
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//读:键盘获取字节,字节->字符。
- while((s= br.readLine())!=null)//读
- {
- if(s.equals("over")) //结束标记
- break;
- String [] str = s.split(",");//切割
- ts.add(new Student( str[0],Integer.parseInt(str[1]),//读一行,装一个学生到集合里去
- Integer.parseInt(str[2]),
- Integer.parseInt(str[3])));
- }
- br.close();
- return ts;
- }
- public static void StuToolWrite(Set<Student> stus) throws IOException
- {
- BufferedWriter bw = new BufferedWriter(new FileWriter("StuInfo.txt"));//写:关联目的地
- for (Student s : stus)
- {
- bw.write(s.toString());
- bw.flush();
- bw.newLine();
- }
- bw.close();
- }
- }
- class Run
- {
- public static void main(String [] args) throws IOException
- {
- Comparator<Student> cmp = Collections.reverseOrder();
- Set<Student> stus = StuTool.StuToolRead(cmp);
- StuTool.StuToolWrite(stus);
- }
- }
复制代码 API:
reverseOrderpublic static <T> Comparator<T> reverseOrder()返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序。(自然顺序是通过对象自身的 compareTo 方法强行排序的。)此方法允许使用单个语句,以逆自然顺序对实现了 Comparable 接口的对象 collection(或数组)进行排序(或维护)。例如,假设 a 是一个字符串数组。那么: Arrays.sort(a, Collections.reverseOrder());将按照逆字典(字母)顺序对数组进行排序。 返回的比较器是可序列化的。
返回: 返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序。 |