lidwup

          java中的排序1:comparable和comparator(轉(zhuǎn))

          分三種情況:

          簡(jiǎn)單類型排序。

          內(nèi)部對(duì)象實(shí)現(xiàn)comparable

          外部對(duì)象實(shí)現(xiàn)comparator

          1、簡(jiǎn)單類型的排序

          簡(jiǎn)單類型不外是byte, char, short, int, long, float, double等數(shù)據(jù)類型,這些類型不能放在聚集中,只能使用數(shù)組。java.util.Arrays方法提供了對(duì)這些類型的sort方法(實(shí)際上還有很多其他有用的方法),下面是對(duì)一個(gè)簡(jiǎn)單的int數(shù)組排序:

                      int[] arr = {2, 3, 1,10,7,4};

                        System.out.print("before sort: ");

                        for (int i = 0; i< arr.length; i++)

                               System.out.print(arr[i] + " ");

                        System.out.println();            

                        Arrays.sort(arr);

                        System.out.print("after sort: ");

                        for (int i = 0; i< arr.length; i++)

                               System.out.print(arr[i] + " ");

                        System.out.println();      

          輸出結(jié)果:

          before sort: 2 3 1 10 7 4

          after sort: 1 2 3 4 7 10

          我們看到排序結(jié)果是按照升序排列的,下面的排序都是如此。

          Comparable & Comparator 都是用來實(shí)現(xiàn)集合中的排序的,只是Comparable是在集合內(nèi)部定義的方法實(shí)現(xiàn)的排序,Comparator是在集合外部實(shí)現(xiàn)的排序,所以,如想實(shí)現(xiàn)排序,就需要在集合外定義Comparator接口的方法或在集合內(nèi)實(shí)現(xiàn)Comparable接口的方法。

          2、內(nèi)部對(duì)象實(shí)現(xiàn)comparable

          案例:

          class Programmer implements Comparable{

                 private String name;

                 private String language;

                 private double pay;

                

                 public Programmer(String name, String language, double pay) {

                        this.name = name;

                        this.language = language;

                        this.pay = pay;

                 }

                 public int compareTo(Object o) {

                        Programmer other = (Programmer)o;

                        return (int)pay - (int)other.pay;

                 }

                 public String toString(){

                        return "{name: " + name + ", language: " + language + ", money: " + pay + "}";

                 }

          }

          對(duì)其進(jìn)行排序:

                        ArrayList list = new ArrayList();

                        list.add(new Programmer("張三", "C", 12000));

                        list.add(new Programmer("李四", "Java", 200));

                        list.add(new Programmer("王五", "C++", 5000));

                        list.add(new Programmer("錢六", "VB", 3000));

                        System.out.println("before sort: " + list);

                        Collections.sort(list);

                        System.out.println("after sort: " + list);

          3、外部對(duì)象實(shí)現(xiàn)comparator

          案例:

          import java.util.Arrays;
          import java.util.Comparator;

          public class SampleComparator implements Comparator {

             public int compare(Object o1, Object o2) {
               return toInt(o1) - toInt(o2);
             }

             private int toInt(Object o) {
               String str = (String) o;
               str = str.replaceAll("一
          ", "1");
               str = str.replaceAll("二
          ", "2");
               str = str.replaceAll("三
          ", "3");
               //
               return Integer.parseInt(str);
             }

             /**
              *
          測(cè)試方法
              */
             public static void main(String[] args) {
               String[] array = new String[] { "
          一二", "三", "" };
               Arrays.sort(array, new SampleComparator());
               for (int i = 0; i < array.length; i++) {
                 System.out.println(array[i]);
               }
             }

          posted on 2009-07-01 15:30 我愛咖啡 閱讀(288) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 德江县| 石台县| 孟津县| 兴安县| 随州市| 高雄县| 隆子县| 四会市| 陇南市| 云龙县| 伊通| 新泰市| 莱芜市| 县级市| 伊金霍洛旗| 柯坪县| 深水埗区| 宿迁市| 顺昌县| 岫岩| 宁陕县| 蓝山县| 盐亭县| 邓州市| 青冈县| 金秀| 荔浦县| 大兴区| 万盛区| 麻阳| 泰和县| 双柏县| 开原市| 韶关市| 延边| 灯塔市| 蛟河市| 九江县| 桓台县| 泰州市| 尤溪县|