ArrayList元素的排序 java 集合

來源:互聯網
上載者:User

標籤:str   this   自動   nbsp   public   技術分享   技術   建構函式   temp   

1.建立項目,建立Man類,添加屬性

2.在代碼編輯地區右鍵,選中source,自動產生setter,getter方法,以及建構函式(類似操作,選中generate constructor using field...)

3.勾選相應的屬性,確認即可,很方便。

 

結果如:

Man.java代碼如下:

 1 public class Man { 2      3     private String name; 4     public String getName() { 5         return name; 6     } 7     public void setName(String name) { 8         this.name = name; 9     }10     public int getHeight() {11         return height;12     }13     public void setHeight(int height) {14         this.height = height;15     }16     private int height;17     18     public Man(String name, int height) {19         super();20         this.name = name;21         this.height = height;22     }23     24     25 26 }

建立Test.java

添加代碼:

import java.util.ArrayList;public class Test {    public static void main(String[] args) {                ArrayList<Man> arr=new ArrayList<>();//建立動態鏈表                //建立4個對象        Man man_exam1=new Man("高個子",185);        Man man_exam2=new Man("中個子",175);        Man man_exam3=new Man("矮個子",165);        Man man_exam4=new Man("高個子",195);                //隨意添加到動態數組裡面        arr.add(man_exam1);        arr.add(man_exam4);        arr.add(man_exam3);            //列印查看        for (Man man : arr) {            System.out.println(man.getName()+man.getHeight());        }                                                    }
View Code

運行輸出如下結果:

高個子185
高個子195
矮個子165

可以看到是亂序的。現在對其排序:

建立類:Compare.java

 

 

 代碼:

 1 import java.util.Comparator; 2  3 public class Compare implements Comparator{ 4  5      public int compare(Object obj0, Object obj1) { 6           Man s1=( Man)obj0; 7           Man s2=( Man)obj1; 8            9           if(s1.getHeight()<=s2.getHeight())10                   return 1;11                   12           return -1; 13      }14      15 }
View Code

在Test.java中加入如下代碼(對象已經存放在動態數組arr裡面,執行如下代碼後arr中已經是排好序的):

 Compare comparator=new Compare();
Collections.sort(arr, comparator);

  

 

 最終完整版Test.java代碼:

import java.util.ArrayList;import java.util.Collections;public class Test {    public static void main(String[] args) {                ArrayList<Man> arr=new ArrayList<>();//建立動態鏈表                //建立4個對象        Man man_exam1=new Man("高個子",185);        Man man_exam2=new Man("中個子",175);        Man man_exam3=new Man("矮個子",165);        Man man_exam4=new Man("高個子",195);                //隨意添加到動態數組裡面        arr.add(man_exam1);        arr.add(man_exam4);        arr.add(man_exam3);            //列印查看        for (Man man : arr) {            System.out.println(man.getName()+man.getHeight());        }               System.out.println("排序後");       Compare comparator=new Compare();       Collections.sort(arr, comparator);               for (int i=0;i<arr.size();i++){        Man user_temp=(Man)arr.get(i);           System.out.println(user_temp.getName()+","+user_temp.getHeight());        }                                }}
View Code

項目結構:

 

ArrayList元素的排序 java 集合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.