隨筆 - 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 馮占科 閱讀(216) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 兴仁县| 虎林市| 铜川市| 玉田县| 襄垣县| 耒阳市| 颍上县| 芦山县| 霸州市| 页游| 阳原县| 焉耆| 定结县| 靖江市| 文昌市| 秦皇岛市| 博爱县| 尼木县| 昌吉市| 互助| 兴海县| 南涧| 玉龙| 六盘水市| 从江县| 嵩明县| 内乡县| 阜宁县| 曲周县| 临高县| 长岛县| 黄骅市| 宁德市| 博客| 金乡县| 方城县| 张掖市| 和龙市| 宁明县| 广灵县| 诸暨市|