标签:rgs etc get tor imp 插入 ati arraylist margin
一、ArrayList
package com.imooc.set; import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { // 用ArrayList存储编程语言的名称,并输出。 //名称包括”Java”、”C”、”C++“、”Go”和”Swift” List list=new ArrayList(); list.add("Java"); list.add("C"); list.add("C++"); list.add("Go"); list.add("Swift"); //输出列表中元素的个数 System.out.println("列表中元素的个数:"+list.size()); //遍历输出所有编程语言 System.out.println("=========================="); for(int i=0;i<list.size();i++) { System.out.print(list.get(i)+" "); } System.out.println(); //移除列表中的C++ System.out.println("=========================="); // list.remove(2); list.remove("C++"); System.out.println("移除c++后列表的元素为:"); for(int i=0;i<list.size();i++) { System.out.print(list.get(i)+" "); } System.out.println(); } }
二、案例
-公告的添加和显示
-在指定位置处插入公告
-删除公告
-修改公告
-编号 id
- 标题 title
-创建人 creator
-创建时间 createTime
-构造方法
-获取和设置属性值的方法
package com.imooc.set; import java.util.Date; public class Notice { //Notice类,属性:id,title,creator,ctreaterDate private int id; private String title; private String creator; private Date creatTime; //构造方法 public Notice(int id, String title, String creator, Date creatTime) { super(); this.id = id; this.title = title; this.creator = creator; this.creatTime = creatTime; } 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 getCreatTime() { return creatTime; } public void setCreatTime(Date creatTime) { this.creatTime = creatTime; } }
package com.imooc.set; import java.util.ArrayList; import java.util.Date; public class NoticeTest { public static void main(String[] args) { // 创建Notice类的对象,生成三条公告 Notice notice1=new Notice(1,"欢迎来到java世界!","管理员",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()); } } }
3、删除、修改公告
package com.imooc.set; import java.util.ArrayList; import java.util.Date; public class NoticeTest { public static void main(String[] args) { // 创建Notice类的对象,生成三条公告 Notice notice1=new Notice(1,"欢迎来到java世界!","管理员",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()); } //再第二条位置新增一条公告 Notice notice4=new Notice(4,"在线编辑器可以使用了","管理员",new Date()); noticeList.add(1,notice4); //显示公告 System.out.println("======================"); 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("======================"); System.out.println("公告内容为:"); for(int i=0;i<noticeList.size();i++) { System.out.println(i+1+":"+((Notice)noticeList.get(i)).getTitle()); } //修改第二条公告的title notice4.setTitle("JAVA在线编辑器可以使用了!"); noticeList.set(1,notice4); //显示公告 System.out.println("======================"); System.out.println("公告内容为:"); for(int i=0;i<noticeList.size();i++) { System.out.println(i+1+":"+((Notice)noticeList.get(i)).getTitle()); } } }
标签:rgs etc get tor imp 插入 ati arraylist margin
原文地址:https://www.cnblogs.com/loveapple/p/11142406.html