隨筆 - 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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 嵊州市| 武胜县| 夏河县| 黄骅市| 高邑县| 左贡县| 和田县| 木兰县| 颍上县| 德保县| 潼关县| 云南省| 定日县| 商南县| 黎平县| 富顺县| 延长县| 民丰县| 元谋县| 永州市| 白城市| 民勤县| 西华县| 德惠市| 额敏县| 台州市| 吴旗县| 孝义市| 尼玛县| 沧州市| 龙口市| 垫江县| 贡山| 巴彦县| 双牌县| 江安县| 德钦县| 台湾省| 西吉县| 峨边| 通道|