LinkedList和ArrayList(指定位置/头尾增加删除)

代码执行结论:
/*
现象:LinkedList在指定位置采用add(index,data)方式增加数据时,位置越靠前耗时越少,越靠后耗时越多(而ArrayList采用add(index,data)方式的耗时跟位置关系不大);
原因:虽说LinkedList底层属于链表数据结构,不需要开辟一块连续的内存地址空间,逻辑上连续即可,在新增、插入和删除操作上占优势(只需要修改节点的前后指向即可,不需要移动数据);
但是因为LinkedList在插入时需要移动指针到指定节点, 才能开始插入,一旦要插入的位置比较远,LinkedList就需要一步一步的移动指针, 直到移动到插入位置;
这就解释了, 为什么节点值越大, 时间越长, 因为指针移动需要时间。而ArrayList是数据结构, 可以根据下标直接获得位置, 这就省去了查找特定节点的时间,所以对ArrayList的影响不是特别大。

现象:LinkedList在头部add数据时(采用add(0,data)和addFirst(data)两种方式耗时差不多,都很少),耗时远远低于ArrayList(采用add(0,data));
原因:不像指定位置一样不需要移动指针,也不需要像ArrayList一样由于连续地址的原因移动数据。

现象:LinkedList在尾部add数据时采用指定位置add(lastIndex,data)的方式,ArrayList在尾部add数据时采用指定位置add(index,data)的方式,则LinkedList耗时远远高于ArrayList;
原因:跟在指定位置add(index,data)数据类似,越靠后LinkedList需要移动指针所花费的时间越多,而ArrayList查找效率本身就很高。

现象:LinkedList在尾部add数据时,如果采用addLast(i)的方式,ArrayList在尾部add数据时采用指定位置add(i)的方式,则LinkedList与ArrayList的耗时差不多;且两者的效率都比使用指定位置的方式有了极大提升
原因:LinkedList不像指定位置的方式那样,不再需要移动指针到指定位置;ArrayList不再像指定位置的方式那样,不再需要查询索引位置。

现象:LinkedList在remove(index)指定位置的数据时,位置越靠前耗时越少,越靠后耗时越多;
原因:跟在指定位置插入数据类似,越靠后移动指针所花费的时间越多。

现象:LinkedList在remove(Object)指定的数据时,耗时远少于ArrayList;
原因:不像指定位置一样不需要移动指针,也不需要像ArrayList一样由于连续地址的原因移动数据。
*/

public class TestArrayList {
	private static final int index = 100000;
	static List<Integer> list = null;
	public static void main(String[] args) {
		//测试ArrayList和LinkedList的插入效率
		addElementInList(list, "ArrayList");
		addElementInList(list, "LinkedList");
		//测试ArrayList和LinkedList的查询效率
		
	}
	
	private static void addElementInList(List<Integer> list, String type){
		if(type == "ArrayList"){
			list = new ArrayList();
			for(int i = 0; i < index; i++){
				list.add(i);
			}
		}
		if(type == "LinkedList"){
			list = new LinkedList();
			for(int i = 0; i < index; i++){
				list.add(i);
			}
		}
		long begin = System.currentTimeMillis();
//		int n = 20000;
		int n = index;
//		int n = 0;
		for(int i = 0; i < index; i++){
			if(type == "LinkedList"){
				list.add(n,i);
//				((LinkedList)list).addLast(i);
//				((LinkedList)list).addFirst(i);
			}else{
				list.add(n,i);
//				list.add(i);
			}
		}
		
		long end = System.currentTimeMillis();
		System.out.printf("在%s集合的索引为%d的位置插入%d条数据,总耗时为%d毫秒\n", type,n, index, end - begin);
		
		
		/*long begin2 = System.currentTimeMillis();
		for(int i = 0; i < index/6; i++){
			if(type == "LinkedList"){
				((LinkedList)list).remove(i);
//				((LinkedList)list).remove((Object)i);
//				((LinkedList)list).remove();
			}else{
				((ArrayList)list).remove(i);
//				((ArrayList)list).remove((Object)i);
			}
		}
		long end2 = System.currentTimeMillis();
		System.out.printf("在%s集合remove(index)%d条数据,总耗时为%d毫秒\n", type, index, end2 - begin2);*/
		
		long begin2 = System.currentTimeMillis();
		for(int i = 0; i < index; i++){
			if(type == "LinkedList"){
//				((LinkedList)list).remove(i);
				((LinkedList)list).remove((Object)i);
//				((LinkedList)list).remove();
			}else{
//				((ArrayList)list).remove(i);
				((ArrayList)list).remove((Object)i);
			}
		}
		long end2 = System.currentTimeMillis();
		System.out.printf("在%s集合remove(Object)%d条数据,总耗时为%d毫秒\n", type, index, end2 - begin2);
	}
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章