JAVA bottom layer (two) ArrayList and LinkList

一、ArrayList

The bottom array structure of ArrayList is an array as shown in the figure below

Initially create an empty array, after adding an element, the size becomes 10, and the expansion is automatically expanded by a multiple of 1.5

Inherit the abstract class abstractList<E> and rewrite methods such as get() and add()

interface

List<E> provides common methods of Lits

RadomAccess is a marked interface that enables classes using this interface to support fast random access

Cloneable provides cloning methods

Serializable provides serialization identification (acting on stream operations)

Advantages: fast query, fast random access is configured using the get method, so the query is fast.

Disadvantages: Adding and deleting are slow, and the delete operation needs to move the entire array again, so it is slow. The expansion operation also leads to slowness.

Thread safety issue: ArrayList thread is not safe, Vetor can be used instead

time complexity

get() reads the index directly, the complexity is O(1)
add(E) adds the element, and adds it directly, the complexity is O(1)
add(index, E) adds the element, in the first few elements Insert later, the latter element needs to be moved backward, the complexity O(n)
remove() deletes the element, the latter element needs to be moved one by one, the complexity O(n)

Summary: Check O(1) Add 0 at the end (1) Middle 0 (n) Delete 0 (n)

二、LinkList

The underlying array structure of LinkList is a two-way circular linked list, and the head and tail of each element store the pointers as shown below

Initially created, no size, no expansion

Inheriting AbstractSqeuentialList supports sequential access by iterators, but does not implement RadomAccess, so random access is not fast

The List interface can perform queue operations on it.
Deque interface, that is, LinkedList can be used as a deque.
The Cloneable interface, which covers the function clone(), can be cloned.
Serializable interface, which means that LinkedList supports serialization and can be transmitted through serialization.

Advantages: Fast addition and deletion, because when inserting and deleting operations, you only need to manipulate references, elements do not need to move elements, they are distributed in different places in the memory, and they are related to each other through references

Disadvantage: It is not as fast as ArrayList query (not a disadvantage)

Thread safety issue: unsafe.
Method 1: List list = Collections.synchronizedList(new LinkedList());
Method 2: Replace all LinkedList with ConcurrentLinkedQueue

time complexity

get() Gets the number of elements and traverses them in turn. The complexity is O(n)
add(E) is added to the end, and the complexity is O(1)
add(index, E). After adding the number of elements, you need to find the first Several elements, direct pointer pointing operation, complexity O(n)
remove() delete elements, direct pointer pointing operation, complexity O(1)

 

Three, summary

  Initial size Expansion inherit achieve advantage Disadvantage Thread safe
ArrayList 0-》10 1.5 abstractList List、RadomAccess、Cloneable 、Serializable  Fast query Slow additions and deletions Not safe, change Vetor
LinkList No Will not expand AbstractSqeuentialList  List 、Deque、Cloneable、Serializable Add and delete fast Not as fast as ArrayList 不安全,1、List list = Collections.synchronizedList(new LinkedList());
2、ConcurrentLinkedQueue

  

Guess you like

Origin blog.csdn.net/qq_37203082/article/details/112652424