当前位置:网站首页>How is LinkedList added?
How is LinkedList added?
2022-07-02 21:28:00 【java. lang.utils】
Problem description :
1,LinkedList When you do the initialization /** * Constructs an empty list. */
public LinkedList() {
}
2,add Method /** * 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, Go to linkLast(e); Method down tracking , The current is the core method of adding /** * Links e as last element. */
void linkLast(E e) {
/* * The first time I added it ,last Is a currently defined Node<E> Object variables * //transient Node<E> last; */
final Node<E> l = last;
/* * Before we create a new Node When the object ,Node In the construction method * The first value is :prev Point to the previous object * Second value :item Currently added element * Third values :next Point to the next object */
final Node<E> newNode = new Node<>(l, e, null);
// After creation Assign to current last
last = newNode;
// Judge l Is it equal to null, so l Namely last The assignment of , In the first addition
// last Namely null therefore l The variable of is null
if (l == null) // Meet the conditions
first = newNode;// Assign a value to first
else
l.next = newNode;
size++;// Index plus
modCount++; // The number of modifications is increased
}
4, This is the result of the first addition 
5, When making the second addition
/** * Links e as last element. */
void linkLast(E e) {
/* * The second time you add ,last It was the last one before Node object */
final Node<E> l = last;
/* * Before we create a new Node When the object ,Node In the construction method * The first value is :prev Point to the previous object , So , When we create the second object * Now this prev Will point to the previous * Second value :item Currently added element * Third values :next Point to the next object */
final Node<E> newNode = new Node<>(l, e, null);
// After creation Assign to current last the last one
last = newNode;
// Judge l Is it equal to null, here l It's the last one Node object
// l It's not equal to null
if (l == null) // Not meeting the conditions
first = newNode;
else
// Let the last Node Of next Point to the newly created node
l.next = newNode;
size++;// Index plus
modCount++; // The number of modifications is increased
}
6, The schematic diagram added for the second time 
7, From this, we can infer , Every time you add a new node , Nodes are appended later , New node's prev Will point to the previous node , The previous node will point to the current new node
8,Node The node is the current static inner class
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;
}
}
边栏推荐
- JS modularization
- I would like to ask what securities dealers recommend? Is it safe to open a mobile account?
- Is it safe to buy funds on securities accounts? Where can I buy funds
- Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition
- Construction and maintenance of business website [3]
- rwctf2022_ QLaaS
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of signal distributors in the global market in 2022
- In depth research and investment feasibility report on the global and China active vibration isolation market 2022-2028
- Happy Lantern Festival! Tengyuanhu made you a bowl of hot dumplings!
- ctf-HCTF-Final-Misc200
猜你喜欢

Add two numbers of leetcode

Hot backup routing protocol (HSRP)

How does esrally perform simple custom performance tests?

Spend more time with your computer on this special holiday, HHH

Unexpectedly, there are such sand sculpture code comments! I laughed

Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition

5 environment construction spark on yarn
![[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)](/img/a7/0b87fa45ef2edd6fac519b40adbeae.gif)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)

Share the easy-to-use fastadmin open source system - Installation

Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
随机推荐
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
Construction and maintenance of business website [5]
Makefile: usage of control functions (error, warning, info)
Backpack template
Plastic granule Industry Research Report - market status analysis and development prospect forecast
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
7. Build native development environment
Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition
When Valentine's Day falls on Monday
Research Report on right-hand front door industry - market status analysis and development prospect forecast
[shutter] statefulwidget component (image component | textfield component)
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
Common authority query instructions in Oracle
[hands on deep learning]02 softmax regression
Review of the latest 2022 research on "deep learning methods for industrial defect detection"
Is it safe to buy funds on securities accounts? Where can I buy funds
Share the easy-to-use fastadmin open source system - Installation
Unexpectedly, there are such sand sculpture code comments! I laughed
[871. Minimum refueling times]
I drew a Gu ailing with characters!