java集合框架入门解析-----linkedList

java集合框架入门解析-----linkedList0.结构图1.linkedList是什么2.linkedList的特点3.LinkedList继承的类和实现的接口0.结构图1.linkedList是什么LinkedList使用了循环双向链表数据结构,并提供了一些队列,栈,双端队列操作的方法。2.linkedList的特点1)与ArrayList对比,LinkedList插入、删除更高效,随机访问速度慢。2)非同步,线程不安全,支持null,有序,元素可重复。3)可以作为栈,队列,双端队
摘要由CSDN通过智能技术生成

0.结构图

在这里插入图片描述

1.linkedList是什么

LinkedList使用了循环双向链表数据结构,并提供了一些队列,栈,双端队列操作的方法。

2.linkedList的特点

1)与ArrayList对比,LinkedList插入、删除更高效,随机访问速度慢。
2)非同步,线程不安全,支持null,有序,元素可重复。
3)可以作为栈,队列,双端队列使用。

3.LinkedList继承的类和实现的接口

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

1)AbstractSequentialList结构及成员函数

public abstract class AbstractSequentialList<E> extends AbstractList<E>

在这里插入图片描述

(1). 什么是 AbstractSequentialList

AbstractSequentialList 继承自 AbstractList,是 LinkedList 的父类,是 List 接口 的简化版实现。
简化的地方在 AbstractSequentialList 只支持按次序访问,而不像 AbstractList 那样支持随机访问。

(2). 获取迭代器


public Iterator<E> iterator() {
   
        return listIterator();
    }
    
public abstract ListIterator<E> listIterator(int index);

(3). add(int, E)

添加元素到指定位置,将当前处于该位置(如果有的话)和任何后续元素的元素移到右边(添加一个到它们的索引):

    public void add(int index, E element) {
   
        try {
   
        //注意:这里要调用了 ListIterator.add(),没有实现该方法的话会报异常
            listIterator(index).add(element);
        } catch (NoSuchElementException exc) {
   
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

(4). addAll(int index, Collection<? extends E> c)

将指定集合中的所有元素插入到列表的指定位置(可选操作)。将当前位于该位置的元素(如果有的话)和任何后续元素向右移动(增加它们的索引)。新元素将按指定集合的迭代器返回的顺序出现在此列表中。如果在操作进行时修改了指定的集合,则此操作的行为未定义。(注意,如果指定的集合是这个列表,并且它不是空的,就会发生这种情况。)

之后请看在linkedList中的实现

(5). get(int index)

获取指定位置的元素:

    public E get(int index) {
   
        try {
   
            return listIterator(index).next();
        } catch (NoSuchElementException exc) {
   
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

AbstractSequentialList 只支持迭代器按顺序 访问,不支持 RandomAccess,所以遍历 AbstractSequentialList 的子类,使用 for 循环 get() 的效率要 <= 迭代器遍历:

for(int i=0, n=list.size(); i &lt; n; i++)
      list.get(i);
//使用迭代器获取元素效率更高些     
for (Iterator i=list.iterator(); i.hasNext(); )
     i.next();

(6). remove(int index)

删除指定位置的元素:

    public E remove(int index) {
   
        try {
   
            ListIterator<E> e = listIterator(index);
            E outCast = e.next();
            e.remove();
            return outCast;
        } catch (NoSuchElementException exc) {
   
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

(7). set(int index, E element)

用指定的元素替换列表中指定位置的元素(可选操作)。

    public E set(int index, E element) {
   
        try {
   
            ListIterator<E> e = listIterator(index);
            E oldVal = e.next();
            e.set(element);
            return oldVal;
        } catch (NoSuchElementException exc) {
   
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

2)LinkedList 的结构和方法

//指向第一个节点的指针。
//只有两种情况: (first == null && last == null) || (first.prev == null && first.item != null)	
transient Node<E> first;

//指向最后一个节点的指针
//(first == null && last == null) || (last.next == null && last.item != null)
transient Node<E> last;

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

(1). 关键的几个内部方法

(头部添加删除,尾部添加删除,获取指定节点,指定节点的添加删除)

注意:要记住
first是始终要指向第一个节点的,有插入头部的节点,或者删除第一个节点的,first要跟着变。
last是始终指向最后一个节点的,有插入,或者删除最后一个节点的,last要跟着变。
判断情况就是上面列举出来的那两种情况。
记住这点,下面的代码就容易看懂了。

//链接e作为第一个元素
private void linkFirst(E e) {
   
		//获取首节点元素的指针
        final Node<E> f = first;
        //新建一个节点,注意next节点指向了F
        final Node<E> newNode = new Node<>(null, e, f);
        //首结点指针指向新的节点
        first = newNode;
        //如果链表为空链表,未节点指针指向新节点
        if (f == null)
            last = newNode;
         //如果不为空,原来首结点的pre指针指向新节点
        else
            f.prev = newNode;
        size++;
        modCount++;
    }
//链接e作为最后一个元素
void linkLast(E e) {
   
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值