1. 程式人生 > >java 對ArrayList排序,實現Comparable介面

java 對ArrayList排序,實現Comparable介面

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Testa 
{
 public static void
 main(String[] args) 
 {
  //傳入引數為檔案目錄  test("d:/a.txt");
 }
 
 public static void test(String filePath){
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader(filePath));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   return;
  }
  
  String []columnName = {"Id", "Name", "Languages", "Math", "English"}; //
列名  int []courseIndexs = {2, 3, 4};          //課程對應的列  int i,j,index;
  String line;
  List<Map<String, Object>> students = new ArrayList<Map<String, Object>>();
  //記錄Id和總值,用於排序  List<Map<String, Object>> sortList = new ArrayList<Map<String, Object>>();
  try
 {
   br.readLine(); //去掉第一行   while((line = br.readLine()) != null){
    index = 0;
    String []se = line.split(" ");
    Map<String, Object> student = new HashMap<String, Object>();
    for(i = 0; i < se.length; i++){
     if("".equals(se[i])){
      continue;
     }
     if(index >= columnName.length){
      continue;
     }
     student.put(columnName[index], se[i]);
     index++;
    }
    //計算平均值,總值    double total = 0;
    for(j = 0; j < courseIndexs.length; j++){
     total += Double.parseDouble((String) student.get(columnName[courseIndexs[j]]));
    }
    double average = total / courseIndexs.length;
    //只取一位小數    average = Math.round(average * 10)/10;
    student.put("Total", total);
    student.put("Average", average);
    
    Map<String, Object> sort = new HashMap<String, Object>();
    sort.put("Id", student.get("Id"));
    sort.put("Total", student.get("Total"));
    sortList.add(sort);
    
    students.add(student);
   }
   br.close();
   
   //選擇排序   for(i = 0; i < sortList.size(); i++){
    for(j = i + 1; j < sortList.size(); j++){
     if((Double)sortList.get(i).get("Total") < (Double)sortList.get(j).get("Total")){
      Map<String, Object> temp = sortList.get(i);
      sortList.set(i, sortList.get(j));
      sortList.set(j, temp);
     }
    }
   }
   Map<Object, Integer> sortedId = new HashMap<Object, Integer>();
   for(i = 0; i < sortList.size(); i++){
    sortedId.put(sortList.get(i).get("Id"), i+1);
   }
   
   //設定序號   for(j = 0; j < students.size(); j++){
    students.get(j).put("Order", sortedId.get(students.get(j).get("Id")));
   }
   
   //輸出(寫到原檔案)
   
//PrintWriter pw = new PrintWriter(new File(filePath));
   
//輸出(寫到其他檔案)   PrintWriter pw = new PrintWriter(new File("D:/b.txt"));
   
   pw.println("Id\tName\tLan\tMath\tEnglish\tAverage\tTotal\tSort");
   int cIndex;
   for(i = 0; i < students.size(); i++){
    Map<String, Object> st = students.get(i);
    cIndex = 0;
    pw.println(st.get(columnName[cIndex++]) + "\t" + st.get(columnName[cIndex++])
      + "\t" + st.get(columnName[cIndex++])+ "\t" + st.get(columnName[cIndex++])
      + "\t" + st.get(columnName[cIndex++])
      + "\t" + st.get("Total")
      + "\t" + st.get("Average")
      + "\t" + st.get("Order"));
   }
   pw.flush();
   pw.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }