Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

JAVA collections list問題@@

利用Collections概念,建立一個member物件要implements Comparable

內建四個屬性分別為int id ,String name,String phone及內會日期

並以list儲存物件

1、分別針對此id name phone對list做排序並印出。

2、利用binarySearch,分別以id 、name、phone為key對list做搜尋並印出結果。

3、對入會日期做排序並搜尋2007年入會的會員,並印出會員資料。

請高手救救我…

1 Answer

Rating
  • Samuel
    Lv 5
    1 decade ago
    Favorite Answer

    1. 這個是典型的Collection用法, 例子程式有點長, 必須放許多段. 都使用內建功能, sort和binarySearch. 把程式全部貼在同一個檔案, 取名為 memberList.java, 編譯即可執行.

    2. 有2個class. 一個是您的member, 一個是測試用主程式, 已經太多字了, 看不懂得地方再用補充問.

    3. 程式

    import java.util.*;

    enum SortKey { ID, NAME, PHONE, SINCE, YEAR }

    class Member implements Comparable {

    public static SortKey skey = SortKey.ID;

    public int id;

    public String name;

    public String phone;

    public Calendar since;

    public Member(int id, String name, String phone, Calendar since) {

    this.id = id;

    this.name = name;

    this.phone = phone;

    this.since = since;

    }

    public int compareTo(Object obj) {

    Member o = (Member)obj;

    switch (skey) {

    case ID :

    return id - o.id;

    case NAME :

    return name.compareTo(o.name);

    case PHONE :

    return phone.compareTo(o.phone);

    case YEAR :

    return since.get(Calendar.YEAR) - o.since.get(Calendar.YEAR);

    default :

    return since.compareTo(o.since);

    }

    }

    }

    public class memberList {

    public static void main(String args[]) {

    String pf = "Id: %d\tName: %s\tPhone: %s\tSince: %tF\n";

    Member mm;

    List<Member> mlist = new ArrayList<Member>();

    mlist.add(new Member(3, "John", "737-9543", getCalendar(2001, 0, 1)));

    mlist.add(new Member(4, "Henry", "626-1234", getCalendar(2008, 5, 25)));

    mlist.add(new Member(1, "Charly", "743-1234", getCalendar(2005, 9, 25)));

    mlist.add(new Member(5, "Oscar", "999-6789", getCalendar(2007, 7, 31)));

    mlist.add(new Member(2, "Mary", "543-6666", getCalendar(1998, 2, 29)));

    System.out.println("未排序資料");

    for (Member m : mlist)

    System.out.printf(pf, m.id, m.name, m.phone, m.since);

    2008-12-24 11:01:39 補充:

    System.out.println("ID 排序資料");

    Member.skey = SortKey.ID;

    Collections.sort(mlist, null);

    for (Member m : mlist)

    System.out.printf(pf, m.id, m.name, m.phone, m.since);

    2008-12-24 11:01:59 補充:

    mm = mlist.get(Collections.binarySearch(mlist, new Member(3, null, null, null), null));

    System.out.printf("搜尋 [ID=3] " + pf + "\n", mm.id, mm.name, mm.phone, mm.since);

    2008-12-24 11:02:21 補充:

    System.out.println("NAME 排序資料");

    Member.skey = SortKey.NAME;

    Collections.sort(mlist, null);

    for (Member m : mlist)

    System.out.printf(pf, m.id, m.name, m.phone, m.since);

    2008-12-24 11:02:40 補充:

    mm = mlist.get(Collections.binarySearch(mlist, new Member(0, "Oscar", null, null), null));

    System.out.printf("搜尋 [NAME=Oscar] " + pf + "\n", mm.id, mm.name, mm.phone, mm.since);

    2008-12-24 11:02:57 補充:

    System.out.println("PHONE 排序資料");

    Member.skey = SortKey.PHONE;

    Collections.sort(mlist, null);

    for (Member m : mlist)

    System.out.printf(pf, m.id, m.name, m.phone, m.since);

    2008-12-24 11:03:11 補充:

    mm = mlist.get(Collections.binarySearch(mlist, new Member(0, null, "743-1234", null), null));

    System.out.printf("搜尋 [PHONE=743-1234] " + pf + "\n", mm.id, mm.name, mm.phone, mm.since);

    2008-12-24 11:03:29 補充:

    System.out.println("入會日期 排序資料");

    Member.skey = SortKey.SINCE;

    Collections.sort(mlist, null);

    for (Member m : mlist)

    System.out.printf(pf, m.id, m.name, m.phone, m.since);

    2008-12-24 11:03:45 補充:

    Member.skey = SortKey.YEAR;

    mm = mlist.get(Collections.binarySearch(mlist, new Member(0, null, null, getCalendar(2007, 0,1)), null));

    System.out.printf("搜尋 [入會日期=2007] " + pf + "\n", mm.id, mm.name, mm.phone, mm.since);

    }

    2008-12-24 11:03:59 補充:

    static Calendar getCalendar(int y, int m, int d) {

    Calendar cal = Calendar.getInstance();

    cal.set(y, m, d);

    return cal;

    }

    }

Still have questions? Get your answers by asking now.