<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年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    我收藏的一些文章!

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

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

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

    * 使用 Collections.sort(List, Comparator)實(shí)現(xiàn),必須實(shí)現(xiàn)Comparator的一個(gè)比較器并復(fù)寫(xiě)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)類來(lái)比較:");
    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[])實(shí)現(xiàn),對(duì)象必須實(shí)現(xiàn)Comparable接口并復(fù)寫(xiě)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[])類來(lái)比較:");
    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)實(shí)現(xiàn),對(duì)象必須實(shí)現(xiàn)Comparable接口并復(fù)寫(xiě)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)類來(lái)比較:");
    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 閱讀(188) 評(píng)論(0)  編輯  收藏 所屬分類: java學(xué)習(xí)
    主站蜘蛛池模板: 亚洲人成影院在线高清| 色猫咪免费人成网站在线观看| 久久水蜜桃亚洲av无码精品麻豆| 亚洲国产精品成人| 免费无码AV片在线观看软件| 暖暖免费在线中文日本| 一个人看的免费视频www在线高清动漫| 亚洲人成网站看在线播放| 久久亚洲AV成人无码| 亚洲AV无码久久精品狠狠爱浪潮| 免费人成在线观看网站视频 | 亚洲中文字幕日产乱码高清app| 免费视频中文字幕| 免费福利网站在线观看| 国内精品久久久久影院免费| 国产福利免费视频 | 在线观看视频免费完整版| 国内精品久久久久影院免费| 中文字幕在线免费看| 国产久爱免费精品视频| 一区二区免费在线观看| 国产hs免费高清在线观看| a毛片在线看片免费| 51午夜精品免费视频| 一道本不卡免费视频| 羞羞漫画登录页面免费| 亚洲av无码av在线播放| 亚洲经典千人经典日产| 久久久久亚洲精品无码网址色欲| 伊人久久五月丁香综合中文亚洲| 亚洲精品V欧洲精品V日韩精品| 毛片基地免费视频a| 黄页网站免费观看| 成年在线观看网站免费| 最近免费中文字幕大全视频| 二个人看的www免费视频| 中文字幕免费播放| a在线观看免费视频| 中文字幕无码免费久久| 最近免费中文在线视频| 欧洲乱码伦视频免费|