在java.util包中那些种提供了List接口的实现
学习笔记,转自:http://blog.****.net/xiangxiaofeng12/article/details/5734770
ArrayList 实现了可变大小的数组,使用它的优点就是遍历元素和随机访问元素效率较高。
使用方法:
List 对象名 = new ArrayList();
ArrayList的常用方法:add(objiect o) ; size () ; get(int index) ;
remove(object o);
----------------------------------------------------------------
public List findListTopic(int page, int boardId) {
List list = new ArrayList();
for(int i = 0 ; i < 20 ; i++){
TopicBean topic = new TopicBean();
topic.setTopicId(i+1);
topic.setTitle("主题"+i+"的标题");
topic.setContent("主题"+i+"的内容");
topic.setPublishTime("2010-7-13 12:12:00");
topic.setUId(i+20);
list.add(topic);
}
return list;
}
public class TopicDaoImplTest {
public static void mian(String [] args){
TopicDao topic = new topicDaoImpl ();
List listTopic = topic.findListTopic( 1,1); //( page, boardId)
for( int i =0; i < listTopic.size (); i++){
Topic topic = (Topic ) listTopic .get (i);
System.out.println("=====主题别表-=====");
System.out.println(topic.getTitle () );
System.out.println(topic.getContent () );
System.out.println(topic.getPublishTime () );
}
}
}
==============================================================================
LinkedList 类和ArrayList类相比,在插入或删除元素时,LinkedList有更好的性能
插入、删除首个元素或最后元素时可以使用LinkedList
用法: List 对象名 = new LinkedList ();
LinkedList 对象名 = new LinkedList (); //使用List没有的方法时
LinkedList常用方法 : addFirst(object o); addLast (object o); get Fiirst();
getLast(); removeFirst();removeLast();
---------------------------------------------------------------------
public List findListReply(int page, int TopicId) {
List list = new LinkedList ();
for(int i=0; i<list.size (); i++) {
ReplyBean reply = new ReplyBean ();
reply.setTopicId(i+1);
reply.setTitle("回复 :"+i+"的标题");
reply.setContent("回复 :"+i+"的内容");
reply.setPublishTime("2010-7=13 16:00:00");
reply.setReplyId(i);
list.add(reply);
}
return list;
}
public class ReplyDaoImplTest{
public static void main (String [] args) {
ReplyDao replyDao = new ReplyDaoImpl ();
List replyList = replyDao.findListReply(1,1);
for ( int i = 0 ; i < replyList.size() ; i++){
Reply reply = ( Reply) replyList.get(i);
System.out.println("=========回复信息==========");
System.out.println( reply.getTitle () );
System.out.println( reply.getContent() );
System.out.println( reply.getPublishTime() );
}
}
}