当前位置:网站首页>How is the LinkedList added?
How is the LinkedList added?
2022-08-06 08:53:00 【This friend from Shanghai】
问题描述:
1,LinkedList 进行初始化的时候 /** * Constructs an empty list. */
public LinkedList() {
}
2,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;
}
3,往linkLast(e);方法下追,当前就是添加的核心方法 /** * Links e as last element. */
void linkLast(E e) {
/* * 第一次添加的时候,last 是当前定义的一个Node<E>对象变量 * //transient Node<E> last; */
final Node<E> l = last;
/* * 在我们创建一个新的Node对象的时候,Node构造方法中 * 第一个值:prev 指向上一个对象 * 第二个值:item 当前添加的元素 * 第三个值:next 指向下一个对象 */
final Node<E> newNode = new Node<>(l, e, null);
// 创建完成后 赋值给当前 last
last = newNode;
// 判断l是否等于null,可见l就是last赋值的,在第一次添加的时候
// last就是null 所以l的变量就是null
if (l == null) // 满足条件
first = newNode;// 赋值给first
else
l.next = newNode;
size++;// 索引加加
modCount++; // 修改次数加加
}
4,第一次添加结果出来就是这样的 
5,当进行第二次添加的时候
/** * Links e as last element. */
void linkLast(E e) {
/* * 第二次添加的时候,last 就是之前上一个Node对象 */
final Node<E> l = last;
/* * 在我们创建一个新的Node对象的时候,Node构造方法中 * 第一个值:prev 指向上一个对象,所以说,我们创建第二个对象时 * 当前这个prev就会指向上一个 * 第二个值:item 当前添加的元素 * 第三个值:next 指向下一个对象 */
final Node<E> newNode = new Node<>(l, e, null);
// 创建完成后 赋值给当前 last 最后一个
last = newNode;
// 判断l是否等于null,此时l等于上一个Node对象
// l不等于null
if (l == null) // 不满足条件
first = newNode;
else
// 让上一个Node的next指向当前新创建的节点
l.next = newNode;
size++;// 索引加加
modCount++; // 修改次数加加
}
6,第二次添加出来的示意图
7,由此内推,每次添加一个新的节点的时候,节点都是往后追加的,新的节点的prev会指向上一个节点,而上一个节点则会指向一个当前这个新的节点
8,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;
}
}
边栏推荐
- 石原子科技正式加入openGauss社区
- 免费的开源的 网页版Xshell【同祝贺大家新年快乐】
- Can the code signing certificate solve the software being alerted by antivirus software?
- 【自动化测试框架】关于unitttest你需要知道的事
- Linux - several ways to install MySQL
- MySQL主外键讲解
- 数组里的值放到另一个数组中,并转大写
- 【内网横移方法与实验】
- Detailed explanation of Mysql things (important)
- Button can only be clicked once
猜你喜欢
随机推荐
CSDN official plug-in
pytorch中 torch.utils.data的用法 ----加载数据篇
Hdu 2022多校训练(5) Slipper
【保姆级教程】腾讯云如何获取secretId和secretKey,以及开通人脸服务
闪烁霓虹灯文字动画
[Mathematical Modeling] Linear Programming
About the third parameter of np.zeros(): c represents similar to c language, row priority; F represents column priority record
MySQL数据库的数据类型和基于MySQL数据类型的综合实例项目
山石发声 | 做好安全运营,没有你想象的那么难
Card hovering frosted glass effect
dalle2: hierarchical text-conditional image generation with clip
流體小球加載動畫
【Log】镜像源配置以及最新可用镜像源
2022 Hailiang SC Travel Notes
QT配置,缺失.dll
rain cloud animation
Xshell download crack, the history of the most simple tutorial
Use the aggird component to implement sliding request paging to achieve the effect of infinite scrolling
2022-08-05: What does the following go code output?A: 65, string; B: A, string; C: 65, int; D: error.
5. 自动引入打包资源 plugins的使用



![[图]Edge 104稳定版发布:引入“增强安全模式”](/img/1a/577a914dd579ee7ce8e2b077e56188.jpg)





