Skip to content

Instantly share code, notes, and snippets.

@nick45chen
Created April 10, 2020 03:14
Show Gist options
  • Save nick45chen/71ed9d46add2b70d9707d6f2a99fc892 to your computer and use it in GitHub Desktop.
Save nick45chen/71ed9d46add2b70d9707d6f2a99fc892 to your computer and use it in GitHub Desktop.
給晟豪
class Main() {
public static void main(String[] arge) {
Notice notice1 = new Notice(4, "課堂規定", "老師", new Date());
Notice notice2 = new Notice(8, "提交作業", "班代", new Date());
Notice notice3 = new Notice(3, "考試詳情", "老師", new Date());
ArrayList<Notice> noticeList = new ArrayList<>(); //加入泛型,讓ArrayList知道陣列裡面的物件類型
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);
System.out.println("未排序公告內容:");
for (Notice n:noticeList)
System.out.println(n);
Collections.sort(noticeList,
new Comparator<Notice>() {
public int compare(Notice n1, Notice n2) {
if (n1.getId() > n2.getId())
return 1;
else
return -1;
}
});
System.out.println("排序後公告內容:");
for (Notice n:noticeList)
System.out.println(n);
}
}
import java.util.Date;
class Notice {
private int id;
private String s1;
private String s2;
public Notice(int i, String s1, String s2, Date date) {
this.id = i;
this.s1 = s1;
this.s2 = s2;
}
public int getId() {
return id;
}
public String getTitle() {
return s1;
}
@Override
public String toString() {
return "Notice{" +
"id=" + id +
", s1='" + s1 + '\'' +
", s2='" + s2 + '\'' +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment