Re: [問題]一個有關於Array的問題

看板java作者時間19年前 (2006/07/19 12:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/6 (看更多)
※ 引述《ogamenewbie.bbs@ptt.cc (._.)》之銘言: > 如果你的 age 不會重複的話... [-----------------------] 這不合理:) > http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html > 加上排序的話... > http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeMap.html import java.util.Arrays; import java.util.Comparator; class Person { Person(String n, int a) { this.name = n; this.age = a; } String name; int age; } public class Test { public static void main(String[] args) { Person[] ps = new Person[] { new Person("Allen", 3), new Person("Emerson", 14), new Person("Eddy", 7) }; Arrays.sort(ps, new Comparator() { public int compare(Object o1, Object o2) { Person p1 = null, p2 = null; if (o1 instanceof Person && o2 instanceof Person) { p1 = (Person) o1; p2 = (Person) o2; } if (p1.age == p2.age) return 0; if (p1.age > p2.age) return 1; else return -1; } }); for (int i = 0; i < ps.length; i++) System.out.println(ps[i].name + ps[i].age); } } -- ┌─────KKCITY─────┐ ◢╱ 只要你通過身份認證 ~ ◥█ bbs.kkcity.com.tw 免經驗、五人連署即開班系板 └──From:163.26.34.214 ──┘ ◥╲ 趕快為班上設個秘密基地吧! --
文章代碼(AID): #14lRN400 (java)
文章代碼(AID): #14lRN400 (java)