日期:2014-05-20  浏览次数:20730 次

新手求JAVA的若干知识点
//运用Collection的范例,将学生成绩按由高到低的顺序排列
package colletiontext5;
import java.util.*;
public class Colletiontext5 
{

//    private static void myComparator() {
//        throw new UnsupportedOperationException("");
//    }
    //注意此处若不加static,Collections.sort(listObj,new myComparator());就会出错
    //non-static variable this cannot be referenced from a sattic context
    //一个静态上下文或者背景或者环境不能引用非静态变量
    public static class myComparator implements Comparator
    {//自动执行决定排序原则的Coparator
       public int compare(Object o1,Object o2)
       {
           //希望由高到低排序
           if(o1 instanceof Grades && o2 instanceof Grades)
           {
               if(((Grades)o1).getSum()<((Grades)o2).getSum())
               return 1;//希望总分低的排在后面
               else if(((Grades)o1).getSum()==((Grades)o2).getSum())
               return 0;
               else 
               return -1;
           }
                 return 0;
        }
        public boolean equals(Object obj)
        {
            return super.equals(obj);
        }
       }
    
    public static void main(String[] args) {
        LinkedList listObj = new LinkedList();//创建一个新的空的 LinkedList
        listObj.add(new Grades(132007,"CYH",new int[]{100,93,89,92}));
        listObj.add(new Grades(319202,"David",new int[]{67,87,76,80}));
        listObj.add(new Grades(217183,"Axel",new int[]{75,93,79,83}));
        listObj.add(new Grades(851924,"Nimo",new int[]{88,98,44,49}));
        listObj.add(new Grades(510018,"fish",new int[]{91,86,99,87}));
        listObj.add(new Grades(701225,"Irving",new int[]{79,98,86,87}));
        listObj.add(new Grades(231198,"林小白",new int[]{28,25,9,39}));
        listObj.add(new Grades(451172,"悟空",new int[]{90,57,79,68}));
        listObj.add(new Grades(431458,"NaNa",new int[]{71,65,79,69}));