ArrayList & LinkedList

一、Collection类的Iterator的用法

1.hasNext()函数的API解释 boolean java.util.Iterator.hasNext()

hasNext boolean hasNext() Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

Returns: true if the iteration has more elements

2.next()函数的API解释 Object java.util.Iterator.next()

next E next() Returns the next element in the iteration. 是从0 开始定位的 Returns: the next element in the iteration Throws: NoSuchElementException - if the iteration has no more elements

Method Summary void add(Object o) Inserts the specified element into the list (optional operation). boolean hasNext() Returns true if this list iterator has more elements when traversing the list in the forward direction. boolean hasPrevious() Returns true if this list iterator has more elements when traversing the list in the reverse direction. Object next() Returns the next element in the list. int nextIndex() Returns the index of the element that would be returned by a subsequent call to next. Object previous() Returns the previous element in the list. int previousIndex() Returns the index of the element that would be returned by a subsequent call to previous. void remove() Removes from the list the last element that was returned by next or previous (optional operation). void set(Object o) Replaces the last element returned by next or previous with the specified element (optional operation). 二、LinkedList类详解 import java.util.*;

public class LinkedListDemo {

public static void main(String []args){
	
	LinkedList  list = new LinkedList();
	
	list.add("one");
	list.add("two");
	list.add("three");		
	System.out.println("<--list中共有 :" + list.size() + "个元素-->");
	System.out.println("<--list中的内容 :" + list + "-->");
	
	String first = (String) list.getFirst();
	String last = (String) list.getLast();
	System.out.println("<--list中第一个元素为 :" + first + "-->");
	System.out.println("<--list中最后一个元素为 :" + last + "-->");
	
	list.addFirst("Begin");
	list.addLast("End");
	System.out.println("<--list中共有 :" + list.size() + "个元素-->");
	System.out.println("<--list中的内容 :" + list + "-->");
	
	System.out.println("<--使用ListIterator接口操作list-->");
	ListIterator lit = list.listIterator();
	System.out.println("<--下一个索引是"+ lit.nextIndex()+ "-->");
	lit.next();
	lit.add("zero");
	lit.previous();
	System.out.println("<--上一个索引是"+ lit.previousIndex()+ "-->");		
	lit.previous();
	System.out.println("<--上一个索引是"+ lit.previousIndex()+ "-->");
	lit.set("Start");
	System.out.println("<--list中的内容 :" + list + "-->");
	
	System.out.println("<--删除list中的zero-->");
	lit.next();
	lit.next();
	lit.remove();
	System.out.println("<--list中的内容 :" + list + "-->");
	
	
	System.out.println("<--删除list中的第一个和最后一个元素-->");
	list.removeFirst();
	list.removeLast();
	
	System.out.println("<--list中共有 :" + list.size() + "个元素-->");
	System.out.println("<--list中的内容 :" + list + "-->");		
}

}

结果: <--list中共有 :3个元素--> <--list中的内容 :[one, two, three]--> <--list中第一个元素为 :one--> <--list中最后一个元素为 :three--> <--list中共有 :5个元素--> <--list中的内容 :[Begin, one, two, three, End]--> <--使用ListIterator接口操作list--> <--下一个索引是0--> <--上一个索引是0--> <--上一个索引是-1--> <--list中的内容 :[Start, zero, one, two, three, End]--> <--删除list中的zero--> <--list中的内容 :[Start, one, two, three, End]--> <--删除list中的第一个和最后一个元素--> <--list中共有 :3个元素--> <--list中的内容 :[one, two, three]-->

三、arrayList & LinkList 区别 一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。 3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。 这一点要看实际情况的。若只对单条数据插入或删除,ArrayList的速度反而优于LinkedList。但若是批量随机的插入删除数据,LinkedList的速度大大优于ArrayList. 因为ArrayList每插入一条数据,要移动插入点及之后的所有数据。 这一点我做了实验。在分别有200000条“记录”的ArrayList和LinkedList的首位插入20000条数据,LinkedList耗时约是ArrayList的20分之1。 for(int m=0;m<20000;m++){ linkedlist.add(m,null); //当在200000条数据之前插入20000条数据时,LinkedList只用了1125多ms.这就是LinkedList的优势所在 } long time4 = new Dte().getTime(); System.out.print("batch linkedlist add:"); System.out.println(time4 - time3); for(int n=0;n<20000;n++){ arraylist.add(n, null); //当在200000条数据之前插入20000条数据时,ArrayList用了18375多ms.时间花费是arraylist的近20倍(视测试时机器性能) } long time5 = new Date().getTime(); System.out.print("batch arraylist add:"); System.out.println(time5 - time4);

4.查找操作indexOf,lastIndexOf,contains等,两者差不多。 5.随机查找指定节点的操作get,ArrayList速度要快于LinkedList. 这里只是理论上分析,事实上也不一定,ArrayList在末尾插入和删除数据的话,速度反而比LinkedList要快。我做过一个插入和删除200000条数据的试验。 long time1 = new Date().getTime(); String s1 = (String) linkedlist.get(100000);// 总记录200000,linkedlist加载第100000条数据耗时15~32ms不等 long time2 = new Date().getTime(); System.out.println(time2 - time1); String s2 = (String) arraylist.get(100000);// 总记录200000,linkedlist加载第100000条数据耗时0ms long time3 = new Date().getTime(); System.out.println(time3 - time2);

/*分别insert200000条数据到linkedlist和arraylist
*由于是在末尾插入数据,arraylist的速度比linkedlist的速度反而要快 
*/
public static void insertList(LinkedList linklist, ArrayList arraylist) {
    long time1 = new Date().getTime();
    System.out.println(time1);
    for (int i = 0; i < 200000; i++) {
        linklist.add(i, "linklist" + i);
    }
    long time2 = new Date().getTime();
    System.out.println(time2 - time1);
    for (int j = 0; j < 200000; j++) {
        arraylist.add(j, "arraylist" + j);
    }
    long time3 = new Date().getTime();
    System.out.println(time3 - time2);
}

/*delete linkedlist和arraylist中的200000条数据 *由于是在末尾删除数据,arraylist的速度比linkedlist的速度反而要快 */ public static void deleteList(LinkedList linklist, ArrayList arraylist) { long time1 = new Date().getTime(); System.out.println(time1); for (int i = 199999; i >= 0; i--) { linklist.remove(i); } long time2 = new Date().getTime(); System.out.println(time2 - time1); for (int j = 199999; j >= 0; j--) { arraylist.remove(j); } long time3 = new Date().getTime(); System.out.println(time3 - time2); }

public static void main(String args[]) {
    LinkedList linkedlist = new LinkedList();
    ArrayList arraylist = new ArrayList();
    insertList(linkedlist, arraylist);

                                    //以下代码省略

插入: LinkedList 578ms ArrayList 437ms 删除: LinkedList 31ms ArrayList 16ms

转载于:https://my.oschina.net/ruibo/blog/185505

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值