LinkedList的增删改查源码分析

  LinkedList是一个双向链表,也可以当做堆栈、队列、双向队列进行操作。

  当你对数据需要进行频繁的增删操作时,你就需要用到LinkedList这个双向链表。
  LinkedList在进行增删操作时,只需要将节点的地址进行修改,而不需要对整个链表进行移动,在增删节点时有很高的效率。
  但是,当查询数据时,程序需要一个个的比对数据来进行查找,效率很低。因此,当链表的数据量很大的时候,删除数据的效率也会降低。因为,remove(Object o)会从此链表中移除首次出现的指定元素,这个移除操作有一个比对的过程,会先在链表中查找指定的元素。


1. LinkedList增加元素的方法

  public boolean add(E e),将指定元素添加到此列表的结尾。此方法等效于public void addLast(E e)

    public boolean add(E e) {
        linkLast(e);
        return true;
    }
    public void addLast(E e) {
        linkLast(e);
    }

  linkLast会判断原来的Last是否为空,如果原来的Last不为空,就将新的Last接到原来的Last的后面。以此实现在列表的结尾添加元素。


  public void add(int index,E element),在此列表中指定的位置插入指定的元素。移动当前在该位置处的元素(如果有),所有后续的元素都向右移(在其索引中添加1)。

    public void add(int index, E element) {
        checkPositionIndex(i<span style="font-family:SimSun;">ndex);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }</span>

  checkPositionIndex(index)用于判断插入位置的合法性,如果该位置是没有元素,则用linkLast添加到队尾。如果有元素,用linkBefore将后续元素都向右移。


  public void addLast(E e),将指定元素添加到此列表的开头。

    public void addFirst(E e) {
        linkFirst(e);
    }
  linkFirst会判断原来的First是否为空,如果原来的First不为空,就将新的First接在原来的First的前面。


2. LinkedList删除元素的方法

  public E remove(),获取并移除此列表的头(第一个元素)。

  public E removrFirst(),移除并返回此列表的第一个元素。

    public E remove() {
        return removeFirst();
    }

    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

  两个方法都是删除链表的第一个元素,第一个返回的是列表的头,第二个返回的是列表的第一个元素。


  public E removeLast(),移除并返回此列表的最后一个元素。

    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }


  public E remove(int index),移除此列表中指定位置的元素。将任何后续元素向左移(从索引中减一)。返回从列表中删除的元素。

    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }

  方法会检查index位置的合法性。然后unlink会保存index位置的元素,将index位置后续的元素全部向左移后,会返回index位置的元素。


  public void clear(),从此列表中移除所有元素。

    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 Iterator
        for (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++;
    }

  方法会把元素全部置为null,最后把列表的大小也修改为0


3.LinkedList查找元素的方法

  public E get(int index)返回此列表中指定位置的元素。
    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

  首先判断位置是否合法,然后遍历到具体位置,获得节点的数据(element)并返回。


  public int indexOf(Object o),返回此列表中首次出现的指定元素的索引,如果此列表中不包含元素,则返回-1。更确切的说,返回满足(o==null ? get(i)==null : o.equals (get(i)))的最低索引i;如果没有此索引,则返回-1。

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


4.LinkedList修改元素的方法

  public E set(int index, E element),将此列表中指定位置的元素替换为指定的元素。 

    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

  方法会先检查位置的合法性。在修改元素前会把原来的元素保存,并在修改后返回。













  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值