List介绍

本文详细解析了List接口的三种实现:ArrayList、LinkedList和Vector的特点及适用场景。对比了它们在不同操作下的性能表现,如插入、删除、查询和遍历等。通过具体代码示例,展示了如何使用这些集合并比较了各种遍历方式的效率。

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+"纳秒");
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值