LinkedList集合类实现新闻新闻管理系统
ArrayList集合类实现新闻新闻管理系统
可以存储各类新闻标题(包含ID、名称、创建者)
可以获取新闻标题的总数
可以逐条打印每条新闻标题的名称
可以添加头条新闻标题
获取头条和最末条新闻标题
可以删除末条新闻标题
public class NewInfo {
private int id;
private String name;
private String creater;
public NewInfo() {
}
public NewInfo(int id, String name, String creater) {
this.id = id;
this.name = name;
this.creater = creater;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreater() {
return creater;
}
public void setCreater(String creater) {
this.creater = creater;
}
@Override
public String toString() {
return "NewInfo{" +
"id=" + id +
", name='" + name + '\'' +
", creater='" + creater + '\'' +
'}';
}
}
public class NewInfoTest {
public static void main(String[] args) {
// 创建对象
NewInfo n1 = new NewInfo(001,"洛阳日报","小A");
NewInfo n2 = new NewInfo(002,"洛阳日报","小B");
NewInfo n3 = new NewInfo(003,"洛阳日报","小C");
// 创建集合对象
ArrayList<NewInfo> list = new ArrayList<>();
list.add(n1);
list.add(n2);
list.add(n3);
// 获取新闻标题的总数
System.out.println("标题总数:"+list.size());
// 逐条打印每条新闻标题的名称
for (int i = 0; i < list.size(); i++) {
NewInfo newInfo = list.get(i);
System.out.println(newInfo);
}
// 添加头条新闻标题
NewInfo n4 = new NewInfo(004,"某日报","小D");
list.add(n4);
System.out.println("====================");
// 获取头条最末条新闻标题
System.out.println(list.get(0));
System.out.println("====================");
// 最末条新闻标题
System.out.println(list.get(list.size()-1));
System.out.println("====================");
// 删除末条新闻标题
boolean remove = list.remove(list.get(list.size() - 1));
System.out.println(remove);
System.out.println(list.get(list.size()-1));
}
}