ArrayList、LinkedList区别(jdk8)
/**
* jdk8
* ArrayList:底层动态数组实现(未初始化指定数组长度)
* add():添加元素时,才初始化数组长度为10。容量不够时,动态扩容策略为: 原容量 + 原容量*0.5
* get():根据数组索引直接查询数组
* remove():根据索引查询数组,然后将其他数据进行前移,最后将最后的那个索引对应值置为Null,等待GC回收
*==================================================================================
* LinkedList: 底层双向链表实现
* Node(Node<E> prev, E element, Node<E> next) {
* this.item = element;
* this.next = next;
* this.prev = prev;
* }
*
* final Node<E> newNode = new Node<>(l, e, null);
* last = newNode;
* if (l == null)
first = newNode;
* else
* l.next = newNode;
*
* =================================================================
* add()方法添加过程:new Node(),将element存放在newNode中,newNode.prev 指向last节点
* newNode.next 指向null
* 判断last节点是否为空,若为null,则 first = last = newNode;
* 否则last.next执行newNode
*
* newNode.prev 绑定 last,last.next 绑定 newNode,实现双向绑定(双向链表)
*
*
* ================================================================
* add(index,element)方法添加过程:
* 判断index是否等于当前集合大小,若等于则直接类似add()方法添加
* 若不等于,则:
* 1、根据index查询将要插入位置的节点 Node node(index)
* 2、linkBefore(element, node(index));
* Node<E> succ = node(index);
*
* Node pred = succ.prev
*
* ******将newNode绑定在succ和succ.prev之间*************
* 将newNode.next = succ
* 将newNode.prev = pred;
*
* ---------将succ.prev,pred.next重新指向newNode,实现双向绑定------
* 将succ.prev = newNode
* 将pred.next = newNode
*
*
* public void add(int index, E element) {
* if (index == size)
* linkLast(element);
* else
* linkBefore(element, node(index));
* }
*
*
* // 找到新插入位置的节点的next或prev
* Node<E> node(int index) {
* // assert isElementIndex(index);
*
* if (index < (size >> 1)) {
* Node<E> x = first;
* for (int i = 0; i < index; i++)
* x = x.next;
* return x;
* } else {
* Node<E> x = last;
* for (int i = size - 1; i > index; i--)
* x = x.prev;
* return x;
* }
* }
*
*
* void linkBefore(E e, Node<E> succ) {
* // assert succ != null;
* final Node<E> pred = succ.prev;
* final Node<E> newNode = new Node<>(pred, e, succ);
* succ.prev = newNode;
* if (pred == null)
* first = newNode;
* else
* pred.next = newNode;
* size++;
* modCount++;
* }
*
* 综上:
* ArrayList:动态数组实现;对于指定位置的添加和删除操作之后,都会有移动元素操作。导致效率低下,而查询则根据索引直接定位,效率高
*
* LinkedList:双向链表实现,对于指定位置的添加和删除操作之后,只需要修改其位置节点执向即可,效率高。而查询则根据节点引用层层调用,效率低下
*
*/
ArrayList、LinkedList区别(jdk8)的更多相关文章
- Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别
ArrayList和Vector的区别ArrayList与Vector主要从二方面来说. 一.同步性: Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...
- LinkedList和ArrayList的区别/何时使用LinkedList和ArrayList
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList ...
- 简谈ArrayList和LinkedList区别
对于ArrayList和LinkedList,他们都实现了List接口,他们的区别大致为: ArrayList LinkedList (1)底层是数组,可以以O(1)的时间复杂度对元素进行随机访问 以 ...
- String[]和ArrayList和LinkedList区别
String[]和ArrayList和LinkedList区别 参考文档如下: http://www.blogjava.net/flysky19/articles/92775.html http:// ...
- LinkedList和ArrayList的区别
LinkedeList和ArrayList都实现了List接口,但是它们的工作原理却不一样.它们之间最主要的区别在于ArrayList是可改变大小的数组,而LinkedList是双向链接串列(doub ...
- Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?
接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...
- List的三个子类ArrayList,LinkedList,Vector区别
一:List的三个子类的特点 ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高.Vector: 底层数据结构是数组,查询快,增删慢. 线程安全,效率低.Vector相对A ...
- Android LinkedList和ArrayList的区别
LinkedeList和ArrayList都实现了List接口,但是它们的工作原理却不一样.它们之间最主要的区别在于ArrayList是可改变大小的数组,而LinkedList是双向链接串列(doub ...
- 一、基础篇--1.2Java集合-Arraylist 与 LinkedList 区别
Arraylist 与 LinkedList 区别 结构上的区别 ArrayList底层实现基于动态数组,LinkedList底层实现基于双向链表. 性能上区别 ArrayList查询快,增删慢 ...
随机推荐
- 【问题】多重继承时,super函数只初始化继承的第一个类,不初始化第二个类。
class A(object): def __init__(self): print("init class A") class B(object): def __init__(s ...
- mysql 使用 limit ,从指定条数读取完,-1失效
, 原因很简单这种写法本身就是错的,虽然它可以在之前的版本中运行(低优先级),新版本的mysql对此做出了修复,现在的替代方法是第二个参数用一个较大的正数代替 比如你写个 , 感觉这方法很蠢,然鹅我并 ...
- 「Flink」配置使用Flink调试WebUI
很多时候,我们在IDE中编写Flink代码,我们希望能够查看到Web UI,从而来了解Flink程序的运行情况.按照以下步骤操作即可,亲测有效. 1.添加Maven依赖 <dependency& ...
- Mac下appium-doctor提示错误汇总
一. 提示 [Error: Could not detect Mac OS X Version from sw_vers output: '10.12'] 解决方法: 1.终端执 ...
- PHP将图片base64编码传输
PHP函数源码 function imgToBase64($img_file) { $img_base64 = ''; if (file_exists($img_file)) { $app_img_f ...
- AGC011-E Increasing Numbers
题意 给定一个数\(n\),\(n≤10^{500,000}\),问\(n\)最少可以拆分成几个不降数的和.一个不降数是在十进制位下,从高位往低位看,每个数都不会比高位的数更小的数 做法 不降数可以拆 ...
- 消息总线:Spring Cloud Stream
最近在学习Spring Cloud的知识,现将消息总线:Spring Cloud Stream 的相关知识笔记整理如下.[采用 oneNote格式排版]
- 清北学堂—2020.1提高储备营—Day 2 afternoon(线段树、树状数组)
qbxt Day 2 afternoon --2020.1.18 济南 主讲:李佳实 目录一览 1.线段树 2.二叉搜索树(略过) 3.树状数组 总知识点:基础数据结构(本人初学感觉好难) 一.线段树 ...
- Linux安装ftp服务-详细步骤
最近项目中用到了FTP服务器,于是整理了一份在Linux服务器上安装FTP的详细步骤供大家分享. 1.首先连接上自己的Linux服务器.我的Linux是CentOS 6 2.检查Linux服务器上是否 ...
- cat - EOF标志的使用
前提 cat命令是用于连接文件并输出到标准输出设备或指定文件中. EOF为标志,可以替换为其他字符串 代码块 ``` 将文件内容作为标准输出也就是将文件内容输出到屏幕中,也可写作 cat filena ...