Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use LinkedList and ArrayList

Shulou Source: shulou.com Published: 2022-06-01 11:34:13 09月23日 Update

This article mainly explains "how to use LinkedList and ArrayList". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use LinkedList and ArrayList.

For the respective usage scenarios of LinkedList and ArrayList, how to confirm which one should be used?

In a nutshell, it is better to use ArrayList in most cases.

Time-consuming has its own advantages and disadvantages. ArrayList has a slight advantage List is just an interface, while LinkedList and ArrayList are different implementations of List. LinkedList's model is a two-way linked list, while ArrayList is a dynamic array.

First of all, compare the algorithm complexity of common operations.

LinkedList

Get (int index): O (n)

Add (E element): O (1)

Add (int index, E element): O (n)

Remove (int index): O (n)

Iterator.remove (): O (1)

Tags: Element length location memory situation capacity advantage dynamic tail array more space move different worst that is content size actual cost Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Xiaomi Apple Redmi