示例代码:
package com.jredu.oopch07;
public class News {
private String time;
private String title;
public News(String time,String title) {
super();
this.time = time;
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
package com.jredu.oopch07; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Ch03 { public static void main(String[] args) { // TODO Auto-generated method stub List list = new LinkedList<>(); News news1 = new News("2011-12-20", "三一重工续写神话"); News news2 = new News("2011-12-20", "陈希慰问三一工作者"); News news3 = new News("2011-12-20", "三一两项产品获评"); News news4 = new News("2011-12-20", "把企业做强为区域发展献力"); News news5 = new News("2011-12-20", "为成果而工作"); News news6 = new News("2011-12-20", "天津6s中心获“用户满意服务明星称号”"); //添加数据带集合 list.add(news1); list.add(news2); list.add(news3); list.add(news4); list.add(news5); list.add(news6); //在列表的首部添加数据 ((LinkedList)list).addFirst(new News("", "新闻标题")); //删除并返回列表中的最后一个元素 ((LinkedList) list).removeLast(); //循环输出集合中的全部数据 for (int i = 0; i < list.size(); i++) { News n = (News)list.get(i); System.out.println(n.getTime()); System.out.println(n.getTitle()); } } }