隨筆 - 24  文章 - 0  trackbacks - 0
          <2011年1月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          //學生類
          package l6;

          import java.util.Comparator;

          public class Student implements Comparable {

           public int num;
           public String name;
           

           public Student(int num, String name) {
            this.num = num;
            this.name = name;
           }

           public int compareTo(Object o) {
            Student s = (Student) o;
            if (num > s.num) {
             return 1;
            } else if (num == s.num) {
             return 0;
            } else {
             return -1;
            }
           }

           public String toString() {
            return "num:" + num + " name:" + name;
           }

           public int compare(Object o1, Object o2) {
            // TODO Auto-generated method stub
            return 0;
           }
          }

          //學生比較器

          package l6;

          import java.util.Comparator;

          public  class StudentComparator implements Comparator {

           public int compare(Object o1, Object o2) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            if (s1.num > s2.num) {
             return 1;
            } else if (s1.num < s2.num) {
             return -1;
            } else {
             return s1.name.compareTo(s2.name);
            }
           }
          }


          //測試類
          package l6;

          import java.util.ArrayList;
          import java.util.Collection;
          import java.util.Collections;
          import java.util.Iterator;

          public class ArrayListTest {

           /**
            * @param args
            */

           public static void main(String[] args) {

            ArrayList<Student> slist = new ArrayList<Student>();
            Student s1 = new Student(1, "z1");
            slist.add(s1);
            Student s3 = new Student(2, "z3");
            slist.add(s3);
            Student s2 = new Student(2, "z2");
            slist.add(s2);
            Student s5 = new Student(2, "z5");
            slist.add(s5);
            Student s4 = new Student(4, "z4");
            slist.add(s4);

             // Collections.sort(slist); //未使用比較器
              Collections.sort(slist, new StudentComparator());//未使用比較器
            PaintE(slist);

           }

           public static void PaintE(Collection c) {
            Iterator it = c.iterator();
            while(it.hasNext()){
             System.out.println(it.next().toString());
            }
            

           }

          }


           

           

          posted on 2011-01-12 23:01 馮占科 閱讀(219) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 勃利县| 延吉市| 六盘水市| 古浪县| 合水县| 清水县| 磐安县| 信宜市| 呼玛县| 赫章县| 五寨县| 开原市| 德钦县| 若尔盖县| 达拉特旗| 龙州县| 慈利县| 昌宁县| 会泽县| 延边| 甘孜县| 封开县| 普兰店市| 开原市| 廊坊市| 西宁市| 新蔡县| 克什克腾旗| 商城县| 安国市| 鹤庆县| 奉化市| 和政县| 盐城市| 甘德县| 门头沟区| 莆田市| 洛扎县| 河曲县| 如东县| 增城市|