LinkedList深入理解

目录

一、与ArrayList类对比梳理

二、LinkedList的定义

2.1、LinkedList的关系类图 

2.2、LinkedList类继承与实现的细节

三、数据存储原理

3.1、内部类Node

3.2、双向链表

四、构造方法 

  LinkedList() 用于创建一个新的空linkedList;

  LinkedList(Collection c) 使用一个集合创建一个新的linkedList。  

实例:

 五、方法的讲解

5.1 查询

5.1.1、get(int index)返回指定位置的元素

5.1.2、获取特殊位置的元素

5.1.3、getLast()获取最后一个元素 

5.1.4、contains(Object o)判断是否含有o元素

5.2 删除元素

5.2.1、removeFirst()删除并返回第一个元素

5.2.2、removeLast()删除并返回最后一个元素。

5.2.3、poll()删除并返回第一个元素。 

5.2.4、remove()删除并返回第一个元素

5.2.5、remove(int index)删除指定位置的元素

5.2.6、remove(Object o) 删除某一个元素,返回是否成功 

5.2.7、clear() 清空链表

5.3 修改元素

set(int index,E element) 设置指定位置的元素。

5.4 增加元素 

5.4.1、add(E e) 链表末尾添加,返回是否成功

5.4.2、 add(int index,E element)向指定位置插入元素

5.4.3、addAll(Collection c),

           将一个集合的所有元素添加到链表后面,返回是否成功

5.4.4、addAll(int index, Collection c)

           将一个集合的所有元素添加到链表的指定位置后面,返回是否成功

5.4.5、addFirst(E e),添加到第一个元素 

5.4.6、addFirst(E e),添加到第一个元素

5.4.7、其余方法(在对象为栈时,用到的方法)

5.5 遍历

5.5.1、for循环遍历

5.5.2、forEach()遍历

5.5.3、迭代器遍历 



一、与ArrayList类对比梳理

list是集合Collection的子接口,是一种有序且允许重复值的单列集合,

实现类为ArrayList类LinkedList类

ArrayList类通过基于Object[ ]数组实现List接口;

LinkedList通过”链表“实现List接口, 采用双向链表结构.

二、LinkedList的定义

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{

2.1、LinkedList的关系类图 

2.2、LinkedList类继承与实现的细节

1.实现Cloneable和Serializable接口主要是为了能够进行深拷贝和序列化。

2.LinkedLisk类继承自AbstractSequentialList。

3.实现了List接口,这个接口定义了一些常见的容器的方法,

4.实现了Deque接口,这个接口可以简单的认为是一个双端队列,两头都可以进可以出,

5.AbstractList抽象类专门用来实现 LinkedList 和ArrayList 中共有的方法,实现代码的复用

    如果直接继承实现的方式会造成冗余,

6.AbstractSequentialList 继承自 AbstractList:

   实现了get(int index)、set(int index, E element)、add(int index, E element)

             和 remove(int index)这些骨干性函数。降低了List接口的复杂度。

   是 LinkedList 的直接父类,并且只支持迭代器按顺序访问

7.是非同步的。

三、数据存储原理

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

3.1、内部类Node

 在LinkedList中有一个一个的Node,这些Node中封装着数据值和地址值,

3.2、双向链表

底层的数据存储结构是基于双向链表实现,在其内部有一个内部类Node 

 既然是双向链表,那么必定存在一种数据结构——我们可以称之为节点,

 节点实例保存数据,前一个节点的位置信息和后一个节点位置信息。

四、构造方法 

  LinkedList() 用于创建一个新的空linkedList;

    public LinkedList() {
    }

  LinkedList(Collection<? extends E> c) 使用一个集合创建一个新的linkedList。  

    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

实例:

	public static void main(String[] args) {
		LinkedList<String> linkedList1 = new LinkedList<>();
        System.out.println(linkedList1);
        String[] arr = {"H", "e", "l", "l", "o"};
        LinkedList<String> linkedList2 = new LinkedList<>(Arrays.asList(arr));
        System.out.println(linkedList2);
	}

 执行结果:

[]
[H, e, l, l, o]

 五、方法的讲解

5.1 查询

5.1.1、get(int index)返回指定位置的元素

    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

5.1.2、获取特殊位置的元素

   getFirst()返回第一个元素

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

  element()返回第一个元素

   public E element() {
        return getFirst();
    }

peek()  返回第一个元素

    public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }

peekFirst()返回头部元素

    public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }

peekLast()返回尾部元素

    public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }

5.1.3、getLast()获取最后一个元素 

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

5.1.4、contains(Object o)判断是否含有o元素

    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }

5.2 删除元素

5.2.1、removeFirst()删除并返回第一个元素

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

5.2.2、removeLast()删除并返回最后一个元素。

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

5.2.3、poll()删除并返回第一个元素。 

    public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

5.2.4、remove()删除并返回第一个元素

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

5.2.5、remove(int index)删除指定位置的元素

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

5.2.6、remove(Object o) 删除某一个元素,返回是否成功 

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

5.2.7、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++;
    }

5.3 修改元素

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

5.4 增加元素 

5.4.1、add(E e) 链表末尾添加,返回是否成功

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

5.4.2、 add(int index,E element)向指定位置插入元素

    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

5.4.3、addAll(Collection<? extends E> c),

           将一个集合的所有元素添加到链表后面,返回是否成功

    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

5.4.4、addAll(int index, Collection<? extends E> c)

           将一个集合的所有元素添加到链表的指定位置后面,返回是否成功

  public boolean addAll(int index, Collection<? extends E> c)

5.4.5、addFirst(E e),添加到第一个元素 

    public void addFirst(E e) {
        linkFirst(e);
    }

5.4.6、addFirst(E e),添加到第一个元素

    public void addLast(E e) {
        linkLast(e);
    }

5.4.7、其余方法(在对象为栈时,用到的方法)

public boolean offer(E e):向链表末尾添加元素,返回是否成功
public boolean offerFirst(E e):头部插入元素,返回是否成功
public boolean offerLast(E e):尾部插入元素,返回是否成功 

5.5 遍历

5.5.1、for循环遍历

        for (int size = linkedList.size(), i = 0; i < size; i++) {
            System.out.println(linkedList.get(i));
        }

5.5.2、forEach()遍历

​
        for (String element: linkedList) {
            System.out.println(element);
        }

​

5.5.3、迭代器遍历 

        Iterator iter = linkedList.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值