Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the LinkedList query method

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use LinkedList query method". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the LinkedList query method.

The bottom layer of LinkedList is based on two-way linked list, and it also implements the List interface, so it also has some characteristics of List (JDK1.7/8 cancels the loop and modifies it to two-way linked list).

New method public boolean add (E e) {

LinkLast (e)

Return true

}

/ * *

* Links e as last element.

, /

Void linkLast (E e) {

Final Node l = last

Final Node newNode = newNode (l, e, null)

Last = newNode

If (l = = null)

First = newNode

Else

L.next = newNode

Size++

ModCount++

}

It can be seen that each insert is a move pointer, which is much more efficient than the copy array of ArrayList.

Query method public E get (int index) {

CheckElementIndex (index)

Return node (index). Item

}

Node node (int index) {

/ / assert isElementIndex (index)

If (index

< (size >

> 1) {

Node x = first

For (int I = 0; I

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

Index; iMurt -)

X = x.prev

Return x

}

}

The above code takes advantage of the characteristics of the two-way linked list. If the index is close to the link header, it traverses from the node header. Otherwise, the traversal starts at the end of the node. Use space (two-way linked list) in exchange for time.

Node () will get a node with the performance of O (nbind 2). If the index value is more than half the size of the linked list, it will traverse from the end node.

This efficiency is very low, especially when the index is closer to the median value of size.

At this point, I believe you have a deeper understanding of "how to use the LinkedList query method". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report