Java LinkedList

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java LinkedList

niuyongzhi   2022-09-29 我要评论

1.LinkedList是基于链表的,而且是一个双向链表,不需要连续内存空间。

 //可以看出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;
        }
    }

2.对于add操作,有两种情况,

1)如果不指定位置,则在末尾添加。只需改变指针指向即可。

 //尾插
    void linkLast(E e) {
        //保存的链表的尾部节点
        final Node<E> l = last;
        //新的节点的前一个node指向链表的最后一个,next 为null
        final Node<E> newNode = newNode < E > (l, e, null);
        last = newNode;
        if (l == null)//如果最后一个节点为null,说明链表是空的
            first = newNode;
        else//如果不为null,last的next节点指向新的节点
            l.next = newNode;
        size++;
        modCount++;
    }

2)如果在指定位置添加,则需要遍历链表。如果索引小于链表长度的一半,从头遍历,否则从尾部遍历。调用node(index)进行遍历。

  public void add(int index, E element) {
        if (index == size)//新插入节点刚好在链表尾部
            linkLast(element);
        else
            linkBefore(element, node(index));
    }
    Node<E> node(int index) {
        //如果index小于链表的一半,则头部遍历
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else { //如果index大于链表的一半,则尾部遍历
            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++;
    }

3.对于get和set方法都需要遍历链表,相比ArrayList,效率要低。

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

4.对于remove,则需要遍历整个链表

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

unlink 断开链表中被删节点前后的指向,建立新的指向。

 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;
        size--;
        modCount++;
        return element;
    }

5.ArrayList分析

ArrayList源码分析

6.总结:

1)对于get和set,Arraylist效率高,直接通过索引取值,而LinkedList,则需要遍历链表。

2)对于Add方法,

如果添加到末尾,差不多。

如果插入在中间,ArrayList需要移动素组,而LinkedList只需操作指针即可,效率要高。但是LinkedList,需要通过索引查找到要插入的位置。

3)对于remove方法。

如果删除在尾部,差不多。

如果删除中间某个元素,ArrayList则需要移动数组。而Linkedlist如果删除的是对象只需要操作指针即可,如果是按索引删除,则需要遍历,找到该元素。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们