arrayList20190625

firefule發表於2021-09-09
import java.util.ArrayList;
import java.util.Date;

//公告管理
public class onLoad {
    public static void main(String[] args) {
        Notice notice1 = new Notice(1, "歡迎來到慕課網", "管理員", new Date());
        Notice notice2 = new Notice(2, "歡迎來到慕課網", "管理員", new Date());
        Notice notice3 = new Notice(3, "歡迎來到慕課網", "管理員", new Date());
        //新增公告
        ArrayList list = new ArrayList();
        list.add(notice1);
        list.add(notice2);
        list.add(notice3);
        //顯示公告
        System.out.println("顯示公告的內容:");
        for (int i = 0; i < list.size(); i++) {
            /*
                list.get(i) 返回值是一個Object , Notice類的有getTitle()方法, 所以進行強制轉換成物件
                Object[] objects=(Object[])list.get(0);
                return objects[3]
            * */
            Notice createObj = (Notice) list.get(i);
            System.out.println(createObj.getId());
            System.out.println(createObj.getTitle());
            System.out.println(createObj.getAdmin());
            System.out.println(createObj.getCreateTime());
        }
    }
}

class Notice {
    private int id;//id
    private String title;//標題
    private String admin;//建立人
    private Date createTime;//建立時間

    public Notice() {
    }

    public Notice(int id, String title, String admin, Date createTime) {
        super();
        this.id = id;
        this.title = title;
        this.admin = admin;
        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 getAdmin() {
        return admin;
    }

    public void setAdmin(String admin) {
        this.admin = admin;
    }

    public Date getCreateTime() {
        return createTime;
    }

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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1834/viewspace-2823184/,如需轉載,請註明出處,否則將追究法律責任。