package altashna.java.arrayList.addData20190701;
import java.util.ArrayList;
import java.util.Date;
public class onload20190701 {
public static void main(String[] args) {
Notice notice1 = new Notice(1, "imooc java", "tahirjan", new Date());
Notice notice2 = new Notice(2, "imooc python", "nurala", new Date());
ArrayList list = new ArrayList();
list.add(notice1);
list.add(notice2);
for (int i = 0; i < list.size(); i++) {
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());
}
Notice notice3 = new Notice(3, "imooc c++", "管理员", new Date());
//末尾添加一条数据, add(int index,Object element) ()
list.add(1, notice3); // 添加第一条公告的后面
System.out.println("添加------");
for (int i = 0; i < list.size(); i++) {
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());
}
System.out.println("删除------根据index方式删除");
list.remove(2);
for (int i = 0; i < list.size(); i++) {
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());
}
System.out.println("修改---------------------");
//修改第二条公告的title内容
notice2.setTitle("hello world");
notice2.setId(0);
//修改第三条公告的title内容
notice3.setTitle("welcome windows");
for (int i = 0; i < list.size(); i++) {
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(int id, String title, String admin, Date createTime) {
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;
}
}
![图片描述](//img1.sycdn.imooc.com//5d1904640001f89d04380234.jpg)
![图片描述](//img1.sycdn.imooc.com//5d19047a0001f89d04380234.jpg)
![图片描述](//img1.sycdn.imooc.com//5d1904940001324e08800514.png)
![图片描述](//img1.sycdn.imooc.com//5d19049c0001933a02660131.jpg)
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦