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

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


          網站導航:
           
          主站蜘蛛池模板: 西贡区| 东明县| 安康市| 崇明县| 竹北市| 韩城市| 荥经县| 赤峰市| 福鼎市| 博湖县| 甘谷县| 烟台市| 南宫市| 龙江县| 商洛市| 黎城县| 图们市| 台东县| 垣曲县| 宣化县| 铅山县| 昌邑市| 毕节市| 云龙县| 盐亭县| 绵阳市| 长沙市| 章丘市| 乡宁县| 和静县| 甘孜县| 祁门县| 二连浩特市| 聂荣县| 炎陵县| 托克逊县| 营山县| 大同市| 正宁县| 昌乐县| 清镇市|