Java List集合使用方法介绍(2)——公告管理

时间:2021-03-25 17:53:47
Java
import java.util.Date;

public class Notice {
private int id;// ID
private String title;// 标题
private String creator;// 创建人
private Date createTime;// 创建时间

public Notice(int id, String title, String creator, Date createTime) {
super();
this.id = id;
this.title = title;
this.creator = creator;
this.createTime = createTime;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getCreator() {
return creator;
}

public void setCreator(String creator) {
this.creator = creator;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

}
Java
import java.util.ArrayList;
import java.util.Date;

public class NoticeTest {

public static void main(String[] args) {
// 创建Notice类的对象,生成三条公告
Notice notice1 = new Notice(1, "欢迎来到慕课网!", "管理员", new Date());
Notice notice2 = new Notice(2, "请同学们按时提交作业!", "老师", new Date());
Notice notice3 = new Notice(3, "考勤通知!", "老师", new Date());

// 添加公告
ArrayList noticeList = new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);

// 显示公告
System.out.println("公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}

System.out.println("***********************************");
// 在第一条公告后面添加一条新公告
Notice notice4 = new Notice(4, "在线编辑器可以使用啦!", "管理员", new Date());
noticeList.add(1, notice4);
// 显示公告
System.out.println("公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
// 删除按时完成作业的公告
noticeList.remove(2);
System.out.println("删除公告后的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}

// 将第二条公告改为:Java在线编辑器可以使用啦!
System.out.println("***********************************");
notice4.setTitle("Java在线编辑器可以使用啦!");
System.out.println("修改后:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}

}

}

Java List集合使用方法介绍(2)——公告管理