<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 100  文章 - 50  trackbacks - 0
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    我收藏的一些文章!

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    Java對象排序的3種實現(xiàn)方法
    2009-05-01 23:13
    /*** Java對象排序的3種實現(xiàn)方式*/

    public class TestObjectSort {
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
     /**方法1

    * 使用 Collections.sort(List, Comparator)實現(xiàn),必須實現(xiàn)Comparator的一個比較器并復寫compare() 方法

      */

        Person1[] ps = new Person1[]{new Person1("p0",0),
    new Person1("p1",3),
    new Person1("p2",5),
    new Person1("p3",4),
    new Person1("p4",8),
    new Person1("p5",6),
    new Person1("p6",7),
    new Person1("p7",1),
    new Person1("p8",2),
    new Person1("p9",9)};
    List<Person1> pl = new ArrayList<Person1>();
    for(int i = 0; i < 10; i++){
    System.out.print(ps[i].getAge());
    pl.add(ps[i]);
    }
    System.out.println("n使用Collections.sort(List, Comparator)類來比較:");
    long l1 = System.currentTimeMillis();
    Collections.sort(pl, new MyComparator());
    System.out.println("time: " + (System.currentTimeMillis() - l1));
    for(Iterator it = pl.iterator(); it.hasNext();){
    Person1 p = (Person1) it.next();
    System.out.print(p.getAge());
    }

    /**方法2

    * 使用Arrays.sort(Object[])實現(xiàn),對象必須實現(xiàn)Comparable接口并復寫compareTo() 方法

      */

        Person2[] ps2 = new Person2[]{new Person2("p0",0),
    new Person2("p1",3),
    new Person2("p2",5),
    new Person2("p3",4),
    new Person2("p4",8),
    new Person2("p5",6),
    new Person2("p6",7),
    new Person2("p7",1),
    new Person2("p8",2),
    new Person2("p9",9)};
    System.out.println("n使用Arrays.sort(Object[])類來比較:");
    long l2 = System.currentTimeMillis();
    Arrays.sort(ps2);
    System.out.println("time: " + (System.currentTimeMillis() - l2));

    for(int i = 0; i < 10; i++){
    System.out.print(ps2[i].getAge());
    }
        /**方法3

      * 使用Collections.sort(List)實現(xiàn),對象必須實現(xiàn)Comparable接口并復寫compareTo()方法

      */

        Person2[] ps3 = new Person2[]{new Person2("p0",0),
    new Person2("p1",3),
    new Person2("p2",5),
    new Person2("p3",4),
    new Person2("p4",8),
    new Person2("p5",6),
    new Person2("p6",7),
    new Person2("p7",1),
    new Person2("p8",2),
    new Person2("p9",9)};
    List<Person2> pl3 = new ArrayList<Person2>();
    for(int i = 0; i < 10; i++){
    pl3.add(ps3[i]);
    }
    System.out.println("n使用Collections.sort(List)類來比較:");
    Collections.sort(pl3);
    for(Iterator it = pl3.iterator(); it.hasNext();){
    Person2 p = (Person2) it.next();
    System.out.print(p.getAge());
    }
    }
    }
    /** * 方法1需要

      */

    class MyComparator implements Comparator{
    public int compare(Object o1, Object o2){
    Person1 p1 = (Person1)o1;
    Person1 p2 = (Person1)o2;
    if(p1.getAge() < p2.getAge()){
    return -1;
    }else if(p1.getAge() == p2.getAge()){
    return 0;
    }else{
    return 1;
    }
    }

    }

      /**

      * 方法1需要

      */

    class Person1{
    private String name;
    private int age;

    public Person1(){}
    public Person1(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }

    }
    /**

    *方法 2,3需要

      */

    class Person2 implements Comparable{
    private String name;
    private int age;

    public Person2(){}
    public Person2(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }

    public int compareTo(Object o){
    Person2 p = (Person2)o;
    if(this.age < p.age){
    return -1;
    }else if(this.age == p.age){
    return 0;
    }else{
    return 1;
    }
    }

    posted on 2009-09-03 17:06 fly 閱讀(195) 評論(0)  編輯  收藏 所屬分類: java學習
    主站蜘蛛池模板: 青青操免费在线视频| 久久久久高潮毛片免费全部播放| 国产精品亚洲mnbav网站 | 亚洲变态另类一区二区三区| 亚洲精品无码av天堂| 24小时日本韩国高清免费| 亚洲av无码片vr一区二区三区| 免费一级毛片正在播放| 国产好大好硬好爽免费不卡| 亚洲国产成人久久综合| 国产亚洲一区二区手机在线观看 | 日本19禁啪啪无遮挡免费动图| 亚洲精品国产日韩无码AV永久免费网| 久久久久亚洲AV无码专区体验| 在线观看亚洲免费| 99精品在线免费观看| 直接进入免费看黄的网站| 亚洲综合在线观看视频| 亚洲国产成人久久一区久久| 2015日韩永久免费视频播放| 国产亚洲综合久久| 精品久久久久久亚洲精品| 久久精品国产精品亚洲| 色妞WWW精品免费视频| 国产免费无码一区二区| 高潮内射免费看片| 亚洲伊人色一综合网| 国产亚洲精品免费视频播放| 四虎成人精品一区二区免费网站| 亚欧免费一级毛片| 亚洲阿v天堂在线2017免费| 亚洲欧美黑人猛交群| 亚洲视频免费在线播放| 亚洲深深色噜噜狠狠爱网站| 手机看片久久国产免费| 免费精品国偷自产在线在线 | 亚洲av无码专区在线观看亚 | 久青草国产免费观看| 亚洲七久久之综合七久久| 亚洲福利视频导航| 国产亚洲AV手机在线观看|