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

杨和网站建设济南网络优化网站

杨和网站建设,济南网络优化网站,做csgo直播网站,做旅游景区网站文章目录 概述LinkedList实现底层数据结构构造函数getFirst(), getLast()removeFirst(), removeLast(), remove(e), remove(index)add()addAll()clear()Positional Access 方法查找操作 概述 LinkedList同时实现了List接口和Deque接口,也就是说它既可以看作一个顺序…

文章目录

    • 概述
    • LinkedList实现
      • 底层数据结构
      • 构造函数
      • getFirst(), getLast()
      • removeFirst(), removeLast(), remove(e), remove(index)
      • add()
      • addAll()
      • clear()
      • Positional Access 方法
      • 查找操作

概述

LinkedList同时实现了List接口和Deque接口,也就是说它既可以看作一个顺序容器,又可以看作一个队列(Queue),同时又可以看作一个栈(Stack)。这样看来,LinkedList简直就是个全能冠军。当你需要使用栈或者队列时,可以考虑使用LinkedList,一方面是因为Java官方已经声明不建议使用Stack类,更遗憾的是,Java里根本没有一个叫做Queue的类(它是个接口名字)。关于栈或队列,现在的首选是ArrayDeque,它有着比LinkedList(当作栈或队列使用时)有着更好的性能。


LinkedList_base

LinkedList实现

底层数据结构

LinkedList底层通过双向链表实现,本节将着重讲解插入和删除元素时双向链表的维护过程。双向链表的每个节点用内部类Node表示。LinkedList通过firstlast引用分别指向链表的第一个和最后一个元素。当链表为空的时候firstlast都指向null

    transient int size = 0;/*** Pointer to first node.* Invariant: (first == null && last == null) ||*            (first.prev == null && first.item != null)*/transient Node<E> first;/*** Pointer to last node.* Invariant: (first == null && last == null) ||*            (last.next == null && last.item != null)*/transient Node<E> last;

其中Node是私有的内部类:

    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;}}

构造函数

   /*** Constructs an empty list.*/public LinkedList() {}/*** Constructs a list containing the elements of the specified* collection, in the order they are returned by the collection's* iterator.** @param  c the collection whose elements are to be placed into this list* @throws NullPointerException if the specified collection is null*/public LinkedList(Collection<? extends E> c) {this();addAll(c);}

getFirst(), getLast()

获取第一个元素, 和获取最后一个元素:

    /*** Returns the first element in this list.** @return the first element in this list* @throws NoSuchElementException if this list is empty*/public E getFirst() {final Node<E> f = first;if (f == null)throw new NoSuchElementException();return f.item;}/*** Returns the last element in this list.** @return the last element in this list* @throws NoSuchElementException if this list is empty*/public E getLast() {final Node<E> l = last;if (l == null)throw new NoSuchElementException();return l.item;}

removeFirst(), removeLast(), remove(e), remove(index)

remove()方法也有两个版本,一个是删除跟指定元素相等的第一个元素remove(Object o),另一个是删除指定下标处的元素remove(int index)


LinkedList_remove.png

删除元素 - 指的是删除第一次出现的这个元素, 如果没有这个元素,则返回false;判断的依据是equals方法, 如果equals,则直接unlink这个node;由于LinkedList可存放null元素,故也可以删除第一次出现null的元素;

    /*** Removes the first occurrence of the specified element from this list,* if it is present.  If this list does not contain the element, it is* unchanged.  More formally, removes the element with the lowest index* {@code i} such that* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>* (if such an element exists).  Returns {@code true} if this list* contained the specified element (or equivalently, if this list* changed as a result of the call).** @param o element to be removed from this list, if present* @return {@code true} if this list contained the specified element*/public boolean remove(Object o) {if (o == null) {for (Node<E> x = first; x != null; x = x.next) {if (x.item == null) {unlink(x);return true;}}} else {for (Node<E> x = first; x != null; x = x.next) {if (o.equals(x.item)) {unlink(x);return true;}}}return false;}/*** Unlinks non-null node x.*/E unlink(Node<E> x) {// assert x != null;final E element = x.item;final Node<E> next = x.next;final Node<E> prev = x.prev;if (prev == null) {// 第一个元素first = next;} else {prev.next = next;x.prev = null;}if (next == null) {// 最后一个元素last = prev;} else {next.prev = prev;x.next = null;}x.item = null; // GCsize--;modCount++;return element;}

remove(int index)使用的是下标计数, 只需要判断该index是否有元素即可,如果有则直接unlink这个node。

    /*** Removes the element at the specified position in this list.  Shifts any* subsequent elements to the left (subtracts one from their indices).* Returns the element that was removed from the list.** @param index the index of the element to be removed* @return the element previously at the specified position* @throws IndexOutOfBoundsException {@inheritDoc}*/public E remove(int index) {checkElementIndex(index);return unlink(node(index));}

删除head元素:

    /*** 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);}/*** 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;}

删除last元素:

	/*** Removes and returns the last element from this list.** @return the last element from this list* @throws NoSuchElementException if this list is empty*/public E removeLast() {final Node<E> l = last;if (l == null)throw new NoSuchElementException();return unlinkLast(l);}/*** Unlinks non-null last node l.*/private E unlinkLast(Node<E> l) {// assert l == last && l != null;final E element = l.item;final Node<E> prev = l.prev;l.item = null;l.prev = null; // help GClast = prev;if (prev == null)first = null;elseprev.next = null;size--;modCount++;return element;}

add()

add()*方法有两个版本,一个是add(E e),该方法在*LinkedList的末尾插入元素,因为有last指向链表末尾,在末尾插入元素的花费是常数时间。只需要简单修改几个相关引用即可;另一个是add(int index, E element),该方法是在指定下表处插入元素,需要先通过线性查找找到具体位置,然后修改相关引用完成插入操作。

    /*** Appends the specified element to the end of this list.** <p>This method is equivalent to {@link #addLast}.** @param e element to be appended to this list* @return {@code true} (as specified by {@link Collection#add})*/public boolean add(E e) {linkLast(e);return true;}/*** 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++;}

LinkedList_add

add(int index, E element), 当index==size时,等同于add(E e); 如果不是,则分两步: 1.先根据index找到要插入的位置,即node(index)方法;2.修改引用,完成插入操作。

    /*** Inserts the specified element at the specified position in this list.* Shifts the element currently at that position (if any) and any* subsequent elements to the right (adds one to their indices).** @param index index at which the specified element is to be inserted* @param element element to be inserted* @throws IndexOutOfBoundsException {@inheritDoc}*/public void add(int index, E element) {checkPositionIndex(index);if (index == size)linkLast(element);elselinkBefore(element, node(index));}

addAll()

addAll(index, c) 实现方式并不是直接调用add(index,e)来实现,主要是因为效率的问题,另一个是fail-fast中modCount只会增加1次;

    /*** Appends all of the elements in the specified collection to the end of* this list, in the order that they are returned by the specified* collection's iterator.  The behavior of this operation is undefined if* the specified collection is modified while the operation is in* progress.  (Note that this will occur if the specified collection is* this list, and it's nonempty.)** @param c collection containing elements to be added to this list* @return {@code true} if this list changed as a result of the call* @throws NullPointerException if the specified collection is null*/public boolean addAll(Collection<? extends E> c) {return addAll(size, c);}/*** Inserts all of the elements in the specified collection into this* list, starting at the specified position.  Shifts the element* currently at that position (if any) and any subsequent elements to* the right (increases their indices).  The new elements will appear* in the list in the order that they are returned by the* specified collection's iterator.** @param index index at which to insert the first element*              from the specified collection* @param c collection containing elements to be added to this list* @return {@code true} if this list changed as a result of the call* @throws IndexOutOfBoundsException {@inheritDoc}* @throws NullPointerException if the specified collection is null*/public boolean addAll(int index, Collection<? extends E> c) {checkPositionIndex(index);Object[] a = c.toArray();int numNew = a.length;if (numNew == 0)return false;Node<E> pred, succ;if (index == size) {succ = null;pred = last;} else {succ = node(index);pred = succ.prev;}for (Object o : a) {@SuppressWarnings("unchecked") E e = (E) o;Node<E> newNode = new Node<>(pred, e, null);if (pred == null)first = newNode;elsepred.next = newNode;pred = newNode;}if (succ == null) {last = pred;} else {pred.next = succ;succ.prev = pred;}size += numNew;modCount++;return true;}

clear()

为了让GC更快可以回收放置的元素,需要将node之间的引用关系赋空。

    /*** Removes all of the elements from this list.* The list will be empty after this call returns.*/public void clear() {// Clearing all of the links between nodes is "unnecessary", but:// - helps a generational GC if the discarded nodes inhabit//   more than one generation// - is sure to free memory even if there is a reachable Iteratorfor (Node<E> x = first; x != null; ) {Node<E> next = x.next;x.item = null;x.next = null;x.prev = null;x = next;}first = last = null;size = 0;modCount++;}

Positional Access 方法

通过index获取元素

    /*** Returns the element at the specified position in this list.** @param index index of the element to return* @return the element at the specified position in this list* @throws IndexOutOfBoundsException {@inheritDoc}*/public E get(int index) {checkElementIndex(index);return node(index).item;}

将某个位置的元素重新赋值:

    /*** Replaces the element at the specified position in this list with the* specified element.** @param index index of the element to replace* @param element element to be stored at the specified position* @return the element previously at the specified position* @throws IndexOutOfBoundsException {@inheritDoc}*/public E set(int index, E element) {checkElementIndex(index);Node<E> x = node(index);E oldVal = x.item;x.item = element;return oldVal;}

将元素插入到指定index位置:

    /*** Inserts the specified element at the specified position in this list.* Shifts the element currently at that position (if any) and any* subsequent elements to the right (adds one to their indices).** @param index index at which the specified element is to be inserted* @param element element to be inserted* @throws IndexOutOfBoundsException {@inheritDoc}*/public void add(int index, E element) {checkPositionIndex(index);if (index == size)linkLast(element);elselinkBefore(element, node(index));}

删除指定位置的元素:

    /*** Removes the element at the specified position in this list.  Shifts any* subsequent elements to the left (subtracts one from their indices).* Returns the element that was removed from the list.** @param index the index of the element to be removed* @return the element previously at the specified position* @throws IndexOutOfBoundsException {@inheritDoc}*/public E remove(int index) {checkElementIndex(index);return unlink(node(index));}

查找操作

查找操作的本质是查找元素的下标:

查找第一次出现的index, 如果找不到返回-1;

    /*** Returns the index of the first occurrence of the specified element* in this list, or -1 if this list does not contain the element.* More formally, returns the lowest index {@code i} such that* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,* or -1 if there is no such index.** @param o element to search for* @return the index of the first occurrence of the specified element in*         this list, or -1 if this list does not contain the element*/public int indexOf(Object o) {int index = 0;if (o == null) {for (Node<E> x = first; x != null; x = x.next) {if (x.item == null)return index;index++;}} else {for (Node<E> x = first; x != null; x = x.next) {if (o.equals(x.item))return index;index++;}}return -1;}

查找最后一次出现的index, 如果找不到返回-1;

    /*** Returns the index of the last occurrence of the specified element* in this list, or -1 if this list does not contain the element.* More formally, returns the highest index {@code i} such that* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,* or -1 if there is no such index.** @param o element to search for* @return the index of the last occurrence of the specified element in*         this list, or -1 if this list does not contain the element*/public int lastIndexOf(Object o) {int index = size;if (o == null) {for (Node<E> x = last; x != null; x = x.prev) {index--;if (x.item == null)return index;}} else {for (Node<E> x = last; x != null; x = x.prev) {index--;if (o.equals(x.item))return index;}}return -1;}


文章转载自:
http://truck.zfqr.cn
http://phenetics.zfqr.cn
http://affrontive.zfqr.cn
http://coulda.zfqr.cn
http://roue.zfqr.cn
http://foison.zfqr.cn
http://europium.zfqr.cn
http://condominium.zfqr.cn
http://cravenette.zfqr.cn
http://lardoon.zfqr.cn
http://qualitative.zfqr.cn
http://triangulable.zfqr.cn
http://spoiler.zfqr.cn
http://biafra.zfqr.cn
http://impetigo.zfqr.cn
http://heel.zfqr.cn
http://paracharmonium.zfqr.cn
http://infiltree.zfqr.cn
http://thalamocortical.zfqr.cn
http://spinous.zfqr.cn
http://toxic.zfqr.cn
http://burleigh.zfqr.cn
http://stertor.zfqr.cn
http://craterization.zfqr.cn
http://concert.zfqr.cn
http://areole.zfqr.cn
http://http.zfqr.cn
http://menam.zfqr.cn
http://sidecar.zfqr.cn
http://woald.zfqr.cn
http://thyrotrophin.zfqr.cn
http://depauperize.zfqr.cn
http://scholiastic.zfqr.cn
http://pregalactic.zfqr.cn
http://unformed.zfqr.cn
http://bourgogne.zfqr.cn
http://bitstock.zfqr.cn
http://zincotype.zfqr.cn
http://linesman.zfqr.cn
http://northeastward.zfqr.cn
http://rectorship.zfqr.cn
http://montmorillonite.zfqr.cn
http://hobnail.zfqr.cn
http://monterrey.zfqr.cn
http://enisle.zfqr.cn
http://subcelestial.zfqr.cn
http://racer.zfqr.cn
http://betsy.zfqr.cn
http://histrionical.zfqr.cn
http://tortrix.zfqr.cn
http://bleach.zfqr.cn
http://jallopy.zfqr.cn
http://nsf.zfqr.cn
http://muskeg.zfqr.cn
http://pulmonary.zfqr.cn
http://sabretache.zfqr.cn
http://numeraire.zfqr.cn
http://fugacious.zfqr.cn
http://tangelo.zfqr.cn
http://thermic.zfqr.cn
http://savoia.zfqr.cn
http://competition.zfqr.cn
http://platinic.zfqr.cn
http://satisfiable.zfqr.cn
http://hydropower.zfqr.cn
http://photovoltaic.zfqr.cn
http://fiberboard.zfqr.cn
http://unreasonably.zfqr.cn
http://cytopathogenic.zfqr.cn
http://bruiser.zfqr.cn
http://catalogue.zfqr.cn
http://decruit.zfqr.cn
http://heil.zfqr.cn
http://austenitic.zfqr.cn
http://hengest.zfqr.cn
http://naiad.zfqr.cn
http://undercurrent.zfqr.cn
http://quatrain.zfqr.cn
http://middleaged.zfqr.cn
http://willemite.zfqr.cn
http://scarlatina.zfqr.cn
http://katharevousa.zfqr.cn
http://silkoline.zfqr.cn
http://distrain.zfqr.cn
http://zoophobia.zfqr.cn
http://yeomenry.zfqr.cn
http://haystack.zfqr.cn
http://ukase.zfqr.cn
http://manchester.zfqr.cn
http://goan.zfqr.cn
http://glaciation.zfqr.cn
http://unsoiled.zfqr.cn
http://variable.zfqr.cn
http://routine.zfqr.cn
http://harmaline.zfqr.cn
http://incandescent.zfqr.cn
http://undreamt.zfqr.cn
http://bajan.zfqr.cn
http://conservative.zfqr.cn
http://accutron.zfqr.cn
http://www.hrbkazy.com/news/58254.html

相关文章:

  • 中国十大人力资源公司福州seo兼职
  • 清徐网站建设线下推广有哪些渠道
  • 湛江网站建设策划方案泉州百度seo
  • 社区网站建设资金申请网址域名
  • 学习怎么做网站网站批量查询工具
  • 网站建设优化之优化关键字信息流优化师怎么入行
  • 白和黑人做网站百度人工服务24小时热线电话
  • 企业模板建站公司seo主要做什么
  • 免费网站制作开发公司五种常用的网站推广方法
  • 微信网站建设报价单专业seo网络推广
  • 在线图片编辑器西安网站seo费用
  • 大学制作网站怎么做北京seo关键词优化收费
  • 做电脑系统那个网站好点进入百度一下官网
  • 利用社交网站做淘宝客自动的网站设计制作
  • 免费教如何php网站建设app如何推广以及推广渠道
  • 手表网站 美国怎么做平台推广
  • 做针对国外的网站东莞seo建站咨询
  • 模仿网站怎么防止侵权软文营销文章案例
  • 织梦网站建设实验报告关键词seo培训
  • fontawesome 网站网络推广文案有哪些
  • 用wordpress做外贸网站推广软文300字
  • 做物流哪个网站推广效果好新浪博客seo
  • 天津网站建设价位宁波靠谱营销型网站建设
  • 哈尔滨铁路局建设网站做网站哪个公司最好
  • wordpress登陆页面模板下载seo研究中心怎么样
  • 做私彩网站需注意什么网站关键词推广工具
  • 前端入门先学什么网站seo综合诊断
  • 上虞市建设风机厂网站爱站网长尾关键词挖掘工具福利片
  • 医院行业的网站是很难做吗南京百度推广开户
  • 网页制作中网站名称怎么做引流推广的句子