List介绍

List 实现了collection接口

ArrayList

  1. 以数组实现的列表,不支持同步
  2. 利用索引位置可以快速定位访问
  3. 不适合指定位置的插入、删除操作
  4. 适合变动不大的,主要用于查询的数据
  5. 和java数组相比,其容量是可以动态调整的
  6. 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

  1. 以双向链表实现的列表,不支持同步
  2. 可被当作堆栈、队列和双端队列进行操作
  3. 顺序访问高效,随机访问较差,中间插入和删除高效
  4. 适用于经常变化的数据
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(同步)

  1. ArrayList类似,可变数组实现的列表
  2. Vector同步,适合在多线程下使用
  3. 原先不属于JCF框架,属于java最早的数据结构,性能较差
  4. 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+"纳秒");
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章