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());
    }
}
原文地址:https://www.cnblogs.com/xiaotaoxu/p/5536523.html