LinkedList源码阅读

本文深入分析了LinkedList的内部实现,包括其成员变量、Node结构、add、remove和get方法。探讨了LinkedList相较于ArrayList在新增和删除操作上的优势,以及其在查询操作上的劣势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

我们之前已经进行过ArrayList的源码阅读,我们知道,同样继承了List接口的LinkedList,比起ArrayList而言,LinkedList在新增元素和删除元素时表显要好,那么今天我们就一起看看LinkedList源码中蕴含了什么秘密。

正文

1、LinkedList的成员变量

我们先看下LinkedList的成员变量:

    transient int size = 0;

这是LinkedList大小,初始值为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;

指向最后一个节点的指针。

    /**
     * The number of times this list has been <i>structurally modified</i>.
     * Structural modifications are those that change the size of the
     * list, or otherwise perturb it in such a fashion that iterations in
     * progress may yield incorrect results.
     *
     * <p>This field is used by the iterator and list iterator implementation
     * returned by the {@code iterator} and {@code listIterator} methods.
     * If the value of this field changes unexpectedly, the iterator (or list
     * iterator) will throw a {@code ConcurrentModificationException} in
     * response to the {@code next}, {@code remove}, {@code previous},
     * {@code set} or {@code add} operations.  This provides
     * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
     * the face of concurrent modification during iteration.
     *
     * <p><b>Use of this field by subclasses is optional.</b> If a subclass
     * wishes to provide fail-fast iterators (and list iterators), then it
     * merely has to increment this field in its {@code add(int, E)} and
     * {@code remove(int)} methods (and any other methods that it overrides
     * that result in structural modifications to the list).  A single call to
     * {@code add(int, E)} or {@code remove(int)} must add no more than
     * one to this field, or the iterators (and list iterators) will throw
     * bogus {@code ConcurrentModificationExceptions}.  If an implementation
     * does not wish to provide fail-fast iterators, this field may be
     * ignored.
     */
    protected transient int modCount = 0;

这个属性继承自LinkedList的父类AbstractList。表示这个list进行结构性修改的次数。

2、Node的内部结构

我们再来看下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;
        }
    }

Node里面包含了三个成员属性:
(1)E item:当前节点的数据。
(2)Node next:当前节点指向下一个节点的指针。
(3)Node prev:当前节点指向上一个节点的指针。
可以看出Node是一个链表结构,链表上的每一个节点都能沿着链表找到头节点和尾节点。
这样的结构与ArrayList不同,ArrayList是由数组来存储元素,利用数组的特性进行一系列操作。在有索引的情况下,进行get和set操作会比较快速些。而LinkedList由于采用链表结构来存储元素,导致在进行查询操作时,需要利用指针的移动来找到目标元素,相对来说性能比较差。

3、add方法

接下来,我们看看LinkedList的add方法:

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

我们看到,在进行新增元素操作时,LinkedList首先调用了linkLast方法:

    /**
     * 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;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

linkLast方法将目标元素作为尾节点放置在链表的末端,并将size和modCount各加1.

4、remove方法

remove方法:

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

找到list中指定的第一个出现的元素,如果存在就删除它。
这里我们可以看到,LinkedList在删除元素时,是对链表进行了遍历,通过移动指针来寻找需要的元素。相比较ArrayList而言,ArrayList通过索引来定位目标元素的方式就直接的多。

5、get方法

get方法:

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

返回list中指定位置的元素。
这里调用了checkElementIndex方法:

    private void checkElementIndex(int index) {
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
    /**
     * Tells if the argument is the index of an existing element.
     */
    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }

检查了目标位置未超出list的大小。
调用node方法:

    /**
     * Returns the (non-null) Node at the specified element index.
     */
    Node<E> node(int index) {
        // assert isElementIndex(index);

        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

返回了指定元素位置的节点对象。
这里LinkedList将链表一分为二,如果目标元素在前半段,那么LinkedList会从前半段链表从头遍历,通过指针的移动来寻找目标元素;否则LinkedList会从链表的尾节点来从后往前遍历,同样通过移动指针去寻找目标元素。
比较而言,ArrayList在进行get操作时,直接通过数组的索引来定位到目标元素,性能上要好些。

内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
管理员功能需求 用户管理 查看用户列表:显示所有用户基本信息 添加用户:支持输入新用户信息并保存至数据库 修改用户信息:支持选择用户并更新其信息 删除用户:支持从数据库中删除选定用户 实习报告成绩管理 查看实习报告列表:显示所有学生实习报告 批阅实习报告:支持选择报告并给出批阅意见和评分 查看实习成绩列表:显示所有学生实习成绩 录入实习成绩:支持选择学生并输入成绩 修改实习成绩:支持更新学生实习成绩 通知公告管理 发布通知公告:支持输入通知内容并发布至系统 管理通知公告:支持查看、编辑和删除已发布通知 教师功能需求 学生管理 查看所指导学生列表:显示教师负责的所有学生 查看学生实习情况:支持查看学生实习岗位、日志、报告等 实习报告批阅成绩录入 查看待批阅报告列表:显示教师待批阅的实习报告 批阅报告:支持选择报告并给出批阅意见和评分 通知公告查看 查看系统通知公告:显示系统发布的所有通知和公告 实习岗位管理 发布实习岗位:支持输入岗位信息并发布至系统 编辑实习岗位:支持更新已发布岗位信息 删除实习岗位:支持从系统中删除选定岗位 学生功能需求 个人信息管理 查看个人信息:显示学生基本信息 修改个人信息:支持更新学生信息 实习岗位查询与申请 查询实习岗位:显示所有可用实习岗位 申请实习岗位:支持选择岗位并提交申请 实习日志提交 提交实习日志:支持输入日志内容并提交至系统 查看实习日志:显示学生提交的实习日志 实习报告提交 提交实习报告:支持输入报告内容并提交至系统 查看实习报告:显示学生报告及批阅意见 通知公告查看 查看系统通知公告:显示系统发布的所有通知和公告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值