List 實現了collection接口
ArrayList
- 以數組實現的列表,不支持同步
- 利用索引位置可以快速定位訪問
- 不適合指定位置的插入、刪除操作
- 適合變動不大的,主要用於查詢的數據
- 和java數組相比,其容量是可以動態調整的
- ArrayList在元素填滿容器的時會自動擴充容器大小的50%
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayList_test {
public static void main(String[] args) {
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(3);
al.add(2);
al.add(1);
al.add(4);
al.add(5);
al.add(6);
al.add(new Integer(6));
System.out.println("This third element is ");
System.out.println(al.get(3));
al.remove(3);
al.add(3,9);
System.out.println("==================遍歷方法=================");
ArrayList<Integer> al2=new ArrayList<Integer>(100000);
for(int i=0;i<100000;i++){
al2.add(i);
}
traverByIterator(al2);
traverByIndex(al2);
traverByFor(al2);
}
public static void traverByIterator(ArrayList<Integer> al){
System.out.println("迭代器遍歷---------------");
long startTime = System.nanoTime();
Iterator iter=al.iterator();
while(iter.hasNext()){
iter.next();
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
public static void traverByIndex(ArrayList<Integer> al){
System.out.println("索引位置遍歷---------------");
long startTime = System.nanoTime();
for(int i=0;i<al.size();i++){
al.get(i);
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
public static void traverByFor(ArrayList<Integer> al){
System.out.println("索引位置遍歷---------------");
long startTime = System.nanoTime();
for(Integer item:al){
;
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
}
ArrayLIist在使用迭代器遍歷的時候速度最慢
———————————————————————————————————————————————————
LinkedList
- 以雙向鏈表實現的列表,不支持同步
- 可被當作堆棧、隊列和雙端隊列進行操作
- 順序訪問高效,隨機訪問較差,中間插入和刪除高效
- 適用於經常變化的數據
import java.util.*;
import java.util.Iterator;
public class LinkedList_test {
public static void main(String[] args) {
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(3);
al.add(2);
al.add(1);
al.add(4);
al.add(5);
al.add(6);
System.out.println(al.size());
al.addFirst(9);
al.add(3,9);
al.remove(3);
System.out.println("==================遍歷方法=================");
LinkedList<Integer> al2=new LinkedList<Integer>();
for(int i=0;i<100000;i++){
al2.add(i);
}
traverByIterator(al2);
traverByIndex(al2);
traverByFor(al2);
}
public static void traverByIterator(LinkedList<Integer> al){
System.out.println("迭代器遍歷---------------");
long startTime = System.nanoTime();
Iterator iter=al.iterator();
while(iter.hasNext()){
iter.next();
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
public static void traverByIndex(LinkedList<Integer> al){
System.out.println("索引位置遍歷---------------");
long startTime = System.nanoTime();
for(int i=0;i<al.size();i++){
al.get(i);
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
public static void traverByFor(LinkedList<Integer> al){
System.out.println("索引位置遍歷---------------");
long startTime = System.nanoTime();
for(Integer item:al){
;
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
}
LinkedList在隨機索引值遍歷最慢
——————————————————————————————————————————————————
Vector(同步)
- 和ArrayList類似,可變數組實現的列表
- Vector同步,適合在多線程下使用
- 原先不屬於JCF框架,屬於java最早的數據結構,性能較差
- 從JDK1.2開始,Vector被重寫,並納入JCF
import java.util.Vector;
import java.util.Enumeration;
import java.util.Iterator;
public class Vector_test {
public static void main(String[] args) {
Vector<Integer> al=new Vector<Integer>();
al.add(3);
al.add(2);
al.add(1);
al.add(4);
al.add(5);
al.add(6);
al.add(new Integer(6));
System.out.println("This third element is ");
System.out.println(al.get(3));
al.remove(3);
al.add(3,9);
System.out.println("==================遍歷方法=================");
Vector<Integer> al2=new Vector<Integer>(100000);
for(int i=0;i<100000;i++){
al2.add(i);
}
traverByIterator(al2);
traverByIndex(al2);
traverByFor(al2);
traverByEnumeration(al2);
}
public static void traverByIterator(Vector<Integer> al){
System.out.println("迭代器遍歷---------------");
long startTime = System.nanoTime();
Iterator iter=al.iterator();
while(iter.hasNext()){
iter.next();
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
public static void traverByIndex(Vector<Integer> al){
System.out.println("索引位置遍歷---------------");
long startTime = System.nanoTime();
for(int i=0;i<al.size();i++){
al.get(i);
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
public static void traverByFor(Vector<Integer> al){
System.out.println("索引位置遍歷---------------");
long startTime = System.nanoTime();
for(Integer item:al){
;
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
public static void traverByEnumeration(Vector<Integer> al){
System.out.println("Enumeration遍歷---------------");
long startTime = System.nanoTime();
for(Enumeration<Integer> enu=al.elements();enu.hasMoreElements();){
enu.nextElement();
}
long endTime =System.nanoTime();
long duration=endTime-startTime;
System.out.println(duration+"納秒");
}
}