ArrayList和LinkedList查询和插入效率问题 您所在的位置:网站首页 linkedlist的pop方法 ArrayList和LinkedList查询和插入效率问题

ArrayList和LinkedList查询和插入效率问题

2023-03-22 18:16| 来源: 网络整理| 查看: 265

一般大家都知道ArrayList和LinkedList大致区别

ArrayList是基于动态数组,LinkedList是基于链表对于随即访问get操作,Arraylist要优于LinkedList,因为LinkedListt需要移动指针对于新增和删除add和removeLinkedList要占优势因为ArrayList需要移动元素 但是事实真的是这样吗?我们来看一段代码 ArrayList arrayList = new ArrayList(); LinkedList linkedList = new LinkedList(); int number = 10000000; new Thread(()->{ StopWatch stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i StopWatch stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i StopWatch stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i StopWatch stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i final Node l = last; final Node newNode = new Node(l, e, null); //新建一个node节点 last = newNode; if (l == null) first = newNode; else //放在最后一个节点 l.next = newNode; size++; modCount++; }

结论:如果在集合最后一位添加元素的时候,ArrayList只需要在最后一位上添加元素,主要的耗时都在数组扩容上,而LinkedList则需要一一遍历到最后一个节点上再添加元素,所以在我们给ArrayList一个非常大的初始容量的时候,大大的减少扩容的次数,这样在大数据量添加的时候ArrayList效率是LinkedList两倍不止



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有