自訂方法實現ArrayList排序

來源:互聯網
上載者:User

標籤:區別   main   pre   i+1   set   for   core   ret   bsp   

 1 package cn.edu.nwpu.java; 2  3 import java.util.ArrayList; 4 import java.util.Collection; 5  6 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections; 7  8 public class MyScore { 9 10     public static void main(String[] args) {11         // 實現選擇、排序、求平均的問題12         // java的數組是定長的,要想實現可程式化的數組需用集合類ArrayList或者vector13         // 兩者的區別是vector是同步的,但是ArrayList是非同步,這個只在多線程中會遇到,非同步更快,同步更安全14         ArrayList<Double> scores = new ArrayList<>();15         // .add()方法用於向Arraylist中添加資料16         scores.add(80.0);17         scores.add(68.9);18         scores.add(70.8);19         scores.add(90.4);20         System.out.println("最高的分數是:" + MyScore.highestScore(scores));21         for(int i = 0; i < scores.size(); i++) {22             System.out.println(scores.get(i));23         }24     }25     26     /**27      * 用於對成績進行排序,然後返回最大值,順利實現自訂的集合排序28      * @param ds 帶排序的數組29      * @return 返回最大值30      */31     public static double highestScore(ArrayList<Double> ds) {32         for(int i = 0; i < ds.size(); i++) {33             for(int j = i+1; j < ds.size(); j++) {34                 if (ds.get(j) > ds.get(i)) {35                     double temp;36                     temp = ds.get(j);37                     ds.set(j, ds.get(i));38                     ds.set(i,temp);39                 }40             }41         }42         return ds.get(0);43     }44 45 }

 

自訂方法實現ArrayList排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.