1. 程式人生 > >用List和Map排序輸出

用List和Map排序輸出

 

參考:java的treemap反序輸出

int->string string->int

java物件陣列的概述

 

import java.util.*;
import java.util.Comparator;

public class StuScore {
    String stno;
    String name_gender;


    public StuScore(String stno, String name_gender) {
        this.stno = stno;
        this.name_gender = name_gender;

    }

    
public String toString() { return stno + " " + name_gender; } public static void main(String[] args) { String[] Name = {"方文娟 女", "王玲霞 女", "孔曉霞 女", "徐雅琴 女", "朱雅琪 女", "史麗君 女", "孫啟蒙 男", "向榮 男", "石敏 女", "李朝那 男", "郭軍 男", "馬元峰 男", "于帥 男", "曹飛飛", "楊麗敏 女", "劉歡 女", "鄧麗君 女", "周杰倫 男",
"王俊凱 男", "王源 男", "易祥千璽 男", "周萍萍 女", "馬茜茜 女", "董麗君 女", "吳亦凡 男", "林俊杰 男", "孔明珠 女", "雷軍 男", "馬雲 男", "東高 男"}; TreeMap<Integer, StuScore> m = new TreeMap<Integer, StuScore>(new Comparator<Integer>(){ public int compare(Integer a, Integer b){
return b-a; } }); Random r = new Random(); StuScore[] stu = new StuScore[30]; for (int i = 0; i < 30; i++) { String sn = String.valueOf(i + 20130301); int g = r.nextInt(60) + 40; StuScore t = new StuScore(sn, Name[i]); m.put(g, t); } Iterator iter = m.keySet().iterator(); while (iter.hasNext()) { Object key = iter.next(); System.out.println(m.get(key) + " " + key); } } }