当前位置:网站首页>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;
}
}
边栏推荐
- [shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
- Activation function - relu vs sigmoid
- Construction and maintenance of business website [5]
- Research Report on market supply and demand and strategy of China's plastic pump industry
- What is the difference between programming in real work and that in school?
- Internet Explorer ignores cookies on some domains (cannot read or set cookies)
- Spend more time with your computer on this special holiday, HHH
- [dynamic planning] p1220: interval DP: turn off the street lights
- Common routines of compressed packets in CTF
- Get weekday / day of week for datetime column of dataframe - get weekday / day of week for datetime column of dataframe
猜你喜欢

rwctf2022_ QLaaS

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
![[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)](/img/17/b5889ec263687aeacf19214785ea8a.jpg)
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)

Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve

The metamask method is used to obtain account information

Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..

5 environment construction spark on yarn

Write the content into the picture with type or echo and view it with WinHex

Roommate, a king of time, I took care of the C language structure memory alignment

I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy
随机推荐
ctf-HCTF-Final-Misc200
Web3js method to obtain account information and balance
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
Makefile: usage of control functions (error, warning, info)
Customized Huawei hg8546m restores Huawei's original interface
Longest public prefix of leetcode
Download vagrant box file locally from Atlas and configuring it
Want to ask, is there any discount for opening an account now? Is it safe to open an account online?
Construction and maintenance of business websites [8]
Research Report on market supply and demand and strategy of China's plastic trunking industry
Common authority query instructions in Oracle
Spend more time with your computer on this special holiday, HHH
Unexpectedly, there are such sand sculpture code comments! I laughed
Analysis of enterprise financial statements [4]
Analysis of enterprise financial statements [3]
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
Research Report on plastic antioxidant industry - market status analysis and development prospect forecast
Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
Go language learning summary (5) -- Summary of go learning notes
Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027