23.LinkedList添加和删除新闻标题
Posted 许先
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了23.LinkedList添加和删除新闻标题相关的知识,希望对你有一定的参考价值。
package entity; public class NewTitle { private int id; //ID private String titleName; //名称 private String creater; //创建者 public NewTitle() { } public NewTitle(int id, String titleName, String creater) { this.id = id; this.titleName = titleName; this.creater = creater; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitleName() { return titleName; } public void setTitleName(String titleName) { this.titleName = titleName; } public String getCreater() { return creater; } public void setCreater(String creater) { this.creater = creater; } }
package test; import java.util.LinkedList; import entity.NewTitle; public class NewTitleDemo { public static void main(String[] args) { // 具体实现步骤 // 1、创建多个各类新闻标题对象 NewTitle car = new NewTitle(1, "汽车", "管理员"); NewTitle medical = new NewTitle(2, "医学", "管理员"); // 2、创建存储各类新闻标题的集合对象 LinkedList newsTitleList = new LinkedList(); // 3、添加头条新闻标题和末尾标题 newsTitleList.addFirst(car); newsTitleList.addLast(medical); // 4、获取头条、以及最末条新闻标题 NewTitle first = (NewTitle) newsTitleList.getFirst(); System.out.println("头条的新闻标题为:" + first.getTitleName()); NewTitle last = (NewTitle) newsTitleList.getLast(); System.out.println("排在最后的新闻标题为:" + last.getTitleName()); // 5、删除头条和最末条新闻标题 NewTitle firstNews=(NewTitle)newsTitleList.removeFirst(); System.out.println("删除的头条新闻标题为:"+firstNews.getTitleName()); NewTitle lastNews=(NewTitle)newsTitleList.removeLast(); System.out.println("删除的末条新闻标题为:"+lastNews.getTitleName()); System.out.println("删除后剩余的新闻条数:"+newsTitleList.size()); } }
以上是关于23.LinkedList添加和删除新闻标题的主要内容,如果未能解决你的问题,请参考以下文章