標籤:區別 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排序