当前位置:网站首页>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;
}
}
边栏推荐
- Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
- Sword finger offer (I) -- handwriting singleton mode
- Record the problems encountered by nodejs asynchronism
- [CV] Wu Enda machine learning course notes | Chapter 12
- AES encryption CBC mode pkcs7padding filling Base64 encoding key 32byte iv16byte
- Welfare | Hupu isux11 Anniversary Edition is limited to hand sale!
- What are the preferential account opening policies of securities companies now? Is it actually safe to open an account online?
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of voltage source converters in the global market in 2022
- Share the easy-to-use fastadmin open source system - Installation
- AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion
猜你喜欢
The metamask method is used to obtain account information
5 environment construction spark on yarn
MySQL learning notes (Advanced)
6 pyspark Library
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
Internal/validators js:124 throw new ERR_ INVALID_ ARG_ Type (name, 'string', value) -- solution
Hot backup routing protocol (HSRP)
I drew a Gu ailing with characters!
rwctf2022_ QLaaS
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
随机推荐
Go language learning summary (5) -- Summary of go learning notes
[hands on deep learning]02 softmax regression
Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
Common authority query instructions in Oracle
Hot backup routing protocol (HSRP)
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)
Analysis of enterprise financial statements [1]
What are the preferential account opening policies of securities companies now? Is it actually safe to open an account online?
Construction and maintenance of business websites [10]
~91 rotation
Research Report on the overall scale, major manufacturers, major regions, products and applications of building automation power meters in the global market in 2022
Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
~90z axis translation
Market trend report, technical innovation and market forecast of China's Micro pliers
Unexpectedly, there are such sand sculpture code comments! I laughed
I drew a Gu ailing with characters!
Research Report on plastic antioxidant industry - market status analysis and development prospect forecast
kernel tty_ struct
5 environment construction spark on yarn
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)