Java:集合框架二(LinkerList)
Java:集合框架二(LinkerList)详解和代码示例:
实例代码:
package cn.kgc.LinkedList;
/**
*
* @author vip宅男 创建日期:2018年01月12号
*
*/
public class News {
/**
* 属性:时间 标题
*/
private String time;
private String title;
/**
* 封装属性
* @return
*/
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/**
* 无惨构造方法
*/
public News(){
}
/**
* 带惨构造
* @param time
* @param title
*/
public News(String time, String title) {
super();
this.time = time;
this.title = title;
}
}
/**
*
* @author vip宅男 创建日期:2018年01月12号
*
*/
public class News {
/**
* 属性:时间 标题
*/
private String time;
private String title;
/**
* 封装属性
* @return
*/
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/**
* 无惨构造方法
*/
public News(){
}
/**
* 带惨构造
* @param time
* @param title
*/
public News(String time, String title) {
super();
this.time = time;
this.title = title;
}
}
package cn.kgc.LinkedList;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author vip宅男 创建日期:2018年01月12号
*
*/
public class Ch02 {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
List list=new LinkedList();
News new1=new News("2011-12-23", "三一重工续写神话");
News new2=new News("2011-10-24", "我们都在学习");
News new3=new News("2011-11-20", "睡觉");
News new4=new News("2011-11-22", "不知道");
News new5=new News("2011-10-21", "看书");
News new6=new News("2011-10-18", "写字");
/**
* 添加数据带集合
*/
list.add(new1);
list.add(new2);
list.add(new3);
list.add(new4);
list.add(new5);
list.add(new6);
/**
* 在列表的首部添加数据
*/
((LinkedList)list).addFirst(new News("", "新闻标题"));
/**
* 循环输出集合中的全部数据
*/
for (int i = 0; i <list.size(); i++) {
News n=(News)list.get(i);
System.out.println(n.getTime());
System.out.println(n.getTitle());
}
}
}
import java.util.LinkedList;
import java.util.List;
/**
*
* @author vip宅男 创建日期:2018年01月12号
*
*/
public class Ch02 {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
List list=new LinkedList();
News new1=new News("2011-12-23", "三一重工续写神话");
News new2=new News("2011-10-24", "我们都在学习");
News new3=new News("2011-11-20", "睡觉");
News new4=new News("2011-11-22", "不知道");
News new5=new News("2011-10-21", "看书");
News new6=new News("2011-10-18", "写字");
/**
* 添加数据带集合
*/
list.add(new1);
list.add(new2);
list.add(new3);
list.add(new4);
list.add(new5);
list.add(new6);
/**
* 在列表的首部添加数据
*/
((LinkedList)list).addFirst(new News("", "新闻标题"));
/**
* 循环输出集合中的全部数据
*/
for (int i = 0; i <list.size(); i++) {
News n=(News)list.get(i);
System.out.println(n.getTime());
System.out.println(n.getTitle());
}
}
}