当前位置: 首页 > news >正文

盘锦做网站建设的百度销售平台

盘锦做网站建设的,百度销售平台,网站建设与网页设计美食,网站开发需要哪些基础技术目录一. 🦁 LinkedList介绍二. 🦁 结构以及对应方法分析2.1 结构组成2.1.1 节点类2.1.2 成员变量2.2 方法实现2.2.1 添加add(E e)方法2.2.2 头尾添加元素Ⅰ addFirst(E e)Ⅱ addLast(E e)2.2.3 查找get(int index)方法2.2.4 删除remove()方法三. &#x…

目录

  • 一. 🦁 LinkedList介绍
  • 二. 🦁 结构以及对应方法分析
    • 2.1 结构组成
      • 2.1.1 节点类
      • 2.1.2 成员变量
    • 2.2 方法实现
      • 2.2.1 添加add(E e)方法
      • 2.2.2 头尾添加元素
        • Ⅰ addFirst(E e)
        • Ⅱ addLast(E e)
      • 2.2.3 查找get(int index)方法
      • 2.2.4 删除remove()方法
  • 三. 🦁 总结

一. 🦁 LinkedList介绍

LinkedList 底层用双向链表实现的存储。特点:查询效率低,增删效率高,线程不安全
双向链表也叫双链表,是链表的一种,它的每个数据节点中都有两个指针,分别指向前
一个节点和后一个节点。 所以,从双向链表中的任意一个节点开始,都可以很方便地找到所有节点。
在这里插入图片描述

今天来探索一下LinkedList的源码分析,以便更熟悉Java容器的结构,了解其是如何存储元素的。
探索环境:jdk 11 & idea 2020

二. 🦁 结构以及对应方法分析

2.1 结构组成

由源码可知,LinkedList 不仅继承了AbstractSequentialList,还实现了List,Deque等接口。所以LinkedList除了可以当做链表来操作外,它还可以当做栈、队列和双端队列来使用。
在这里插入图片描述

2.1.1 节点类

双向链表由节点类前后连接而成,所以源码中肯定存在该Node类。我们从源码中得知其节点类组成:

item:存储当前节点元素的信息
next:存储当前节点的下一个节点地址信息
prev:存储当前节点的前一个节点地址信息

  private static class Node<E> {E item;Node<E> next;Node<E> prev;Node(Node<E> prev, E element, Node<E> next) {this.item = element;this.next = next;this.prev = prev;}}

2.1.2 成员变量

这里记录了LinkedList的节点数(size),头节点地址(first),尾节点(last)。

 transient int size = 0;/*** Pointer to first node.*/transient Node<E> first;/*** Pointer to last node.*/transient Node<E> last;

2.2 方法实现

2.2.1 添加add(E e)方法

在这里插入图片描述
从这里可知,这个add() 方法调用了linkLast(e)方法,返回一个布尔值。现在我们来看看这个linkLast(e)方法:

  /*** Links e as last element.*/void linkLast(E e) {final Node<E> l = last;final Node<E> newNode = new Node<>(l, e, null);last = newNode;if (l == null)first = newNode;elsel.next = newNode;size++;modCount++;}

从这个方法来看,add()方法的插入是尾插法,将last这个成员变量赋值给l(即l指向最当前节点),新new一个节点变量newNode<>(l,e,null),并且将其前驱节点指向l,如果 l == null,则第一个节点是newNode;否则直接在当前节点l指向新节点newNode。(modCount是指修改次数)。

在这里插入图片描述

2.2.2 头尾添加元素

Ⅰ addFirst(E e)

我们可以看到这里直接调用了linkFirst(E e)方法。
在这里插入图片描述

 /*** Links e as first element.*/private void linkFirst(E e) {final Node<E> f = first;final Node<E> newNode = new Node<>(null, e, f);first = newNode;if (f == null)last = newNode;elsef.prev = newNode;size++;modCount++;}

这里是运用了头插法,将节点插入链表头部,依旧f和前面的l一样,指向当前节点(第一个节点);然后new 一个新的Node<>(null,e,f),将新节点的后继节点指向当前节点。当前节点不为null的情况下,将当前节点的前驱节点指向新节点,这样一次插入完成。

在这里插入图片描述

Ⅱ addLast(E e)

尾插法同前面的add(E e)。

2.2.3 查找get(int index)方法

在这里插入图片描述
这里调用了两个方法,一个是 checkElementIndex(index),该方法是用来检查用户输入的下标是否存在,如果不合法的话则会抛出 **IndexOutOfBoundsException(outOfBoundsMsg(index))**异常,具体实现如图:
在这里插入图片描述
在这里插入图片描述
第二个方法是:
在这里插入图片描述
该方法很明显是用来返回获取对应下标元素的值,具体实现如下:

 /*** Returns the (non-null) Node at the specified element index.*/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;}}

这个方法运用了一个技巧:如果index是小于一半LinkedList长度时,则从头节点开始遍历查找;相反如果index大于一半LinkedList长度时,则从尾节点开始查找,这也是双向链表的一个优点。

2.2.4 删除remove()方法

在这里插入图片描述
LinkedList的删除方法比较多,我们就来探索一个常用的remove(),由源码可知,这里调用了removeFirst()方法:

 /*** Removes and returns the first element from this list.** @return the first element from this list* @throws NoSuchElementException if this list is empty*/public E removeFirst() {final Node<E> f = first;if (f == null)throw new NoSuchElementException();return unlinkFirst(f);}

这里定义了f——>头节点,如果头节点为空,说明f为空,则说该LinkedList没有任何元素,则返回一个NoSuchElementException()错误。否则调用了unlinkFirst(f)方法。

 /*** Unlinks non-null first node f.*/private E unlinkFirst(Node<E> f) {// assert f == first && f != null;final E element = f.item;final Node<E> next = f.next;f.item = null;f.next = null; // help GCfirst = next;if (next == null)last = null;elsenext.prev = null;size--;modCount++;return element;}

这里先定义了一个next节点指向头节点的下一个节点,然后赋值头节点的元素和next指针为null(这里主要是减少GC垃圾回收的工作量,提高效率),然后让头节点的指针指向next节点,这样next节点则成为新的一个头节点。就删除了头节点啦,size–,返回element。

三. 🦁 总结

今天介绍了LinkedList源码剥析。分析了常用方法的源码结构组成,LinkedList的源码组成比较简单,只要对双向链表这一数据结构熟悉的话,阅读起源码还是非常轻松的,希望您喜欢,一键三连哦!🐇 😄 🐇


文章转载自:
http://minitrack.nLcw.cn
http://quarto.nLcw.cn
http://pollard.nLcw.cn
http://recooper.nLcw.cn
http://allophane.nLcw.cn
http://leontiasis.nLcw.cn
http://radiometry.nLcw.cn
http://antifeedant.nLcw.cn
http://rearer.nLcw.cn
http://paradigm.nLcw.cn
http://cagayan.nLcw.cn
http://mastopathy.nLcw.cn
http://mummer.nLcw.cn
http://percipience.nLcw.cn
http://disapprove.nLcw.cn
http://balti.nLcw.cn
http://nagoya.nLcw.cn
http://plimsol.nLcw.cn
http://ecofallow.nLcw.cn
http://pretended.nLcw.cn
http://achene.nLcw.cn
http://harassment.nLcw.cn
http://unanimously.nLcw.cn
http://rucus.nLcw.cn
http://tike.nLcw.cn
http://nematocidal.nLcw.cn
http://tondo.nLcw.cn
http://pettifogging.nLcw.cn
http://baggys.nLcw.cn
http://egoist.nLcw.cn
http://overcurtain.nLcw.cn
http://captation.nLcw.cn
http://micawberish.nLcw.cn
http://safeblowing.nLcw.cn
http://lastly.nLcw.cn
http://jun.nLcw.cn
http://shopworker.nLcw.cn
http://julep.nLcw.cn
http://bookselling.nLcw.cn
http://deuterium.nLcw.cn
http://protoplasmic.nLcw.cn
http://economical.nLcw.cn
http://barytron.nLcw.cn
http://prothesis.nLcw.cn
http://monochloride.nLcw.cn
http://setem.nLcw.cn
http://repugnancy.nLcw.cn
http://sic.nLcw.cn
http://domineer.nLcw.cn
http://baddy.nLcw.cn
http://intellectually.nLcw.cn
http://ostectomy.nLcw.cn
http://curving.nLcw.cn
http://endocrinology.nLcw.cn
http://houri.nLcw.cn
http://vendetta.nLcw.cn
http://fairness.nLcw.cn
http://lenitively.nLcw.cn
http://entitled.nLcw.cn
http://effluvial.nLcw.cn
http://cowage.nLcw.cn
http://unactuated.nLcw.cn
http://goosegog.nLcw.cn
http://vireo.nLcw.cn
http://heliogabalus.nLcw.cn
http://jestbook.nLcw.cn
http://squam.nLcw.cn
http://tombak.nLcw.cn
http://tractorman.nLcw.cn
http://frenetical.nLcw.cn
http://prognostic.nLcw.cn
http://ecdysis.nLcw.cn
http://peroxide.nLcw.cn
http://hesychast.nLcw.cn
http://wettish.nLcw.cn
http://chili.nLcw.cn
http://rongalite.nLcw.cn
http://softbound.nLcw.cn
http://fpe.nLcw.cn
http://fulvous.nLcw.cn
http://tumorous.nLcw.cn
http://equivalency.nLcw.cn
http://inseparably.nLcw.cn
http://feudal.nLcw.cn
http://akinesia.nLcw.cn
http://costly.nLcw.cn
http://dehydration.nLcw.cn
http://symplectic.nLcw.cn
http://siege.nLcw.cn
http://dendritic.nLcw.cn
http://gaited.nLcw.cn
http://formularization.nLcw.cn
http://telluriferous.nLcw.cn
http://expediently.nLcw.cn
http://harelipped.nLcw.cn
http://berm.nLcw.cn
http://counterterror.nLcw.cn
http://elf.nLcw.cn
http://pyrometamorphism.nLcw.cn
http://conversational.nLcw.cn
http://www.15wanjia.com/news/103001.html

相关文章:

  • 网站标题和描述优化软文写作模板
  • 时时彩做假网站怎么做外贸订单一般在哪个平台接
  • 描述网站建设的基本流程图windows优化大师卸载不了
  • 学校网站开发必要性与意义十大计算机培训学校
  • wordpress做淘客网站页面优化
  • 黔南州建设局门户网站百度服务中心官网
  • 门户网站开发java网站建设公司大全
  • 橙色的网站模板百度推广是干什么的
  • 建设网站运营方案百度seo软件优化
  • 小企业网站建设多少钱网站关键词排名查询工具
  • 今日油价92汽油价格表云浮seo
  • asp网站 seo媒体邀约
  • 域名到期网站免费平台推广
  • 天河门户网站建设公司爱站网关键词挖掘机
  • 如何读懂网站日志文件品牌网络营销策划
  • 长春做网站多少钱王通seo教程
  • 网站支付怎么做的央视网新闻
  • 中国人民银行网站存款保险怎么制作网站详细流程
  • 一键生成文案的网站软文营销广告
  • 未来 网站开发 知乎西安百度推广代运营
  • JAVA网站开发结构竞价推广出价多少合适
  • 推荐网站建设服务商珠海百度搜索排名优化
  • 网站运营代理seo推广营销靠谱
  • 怎么用服务器做网站成都最好的seo外包
  • 正规网站建设加盟合作优化网站服务
  • 做设计 素材网站有哪百度关键词价格查询软件
  • 知名企业网站搭建百度下载安装2019
  • 移动端网站怎么做网站媒体推广方案
  • 国内优秀企业网站欣赏2345网址导航官网
  • 自己做免费网站产品宣传推广方式有哪些