当前位置:网站首页>arrayList && linkedList
arrayList && linkedList
2022-06-13 05:46:00 【Coffee is not bitter**】
List of articles
A long time ago, I wrote an article about two list Performance articles Performance comparison test
This article goes back to the bottom , Let's take a look at two list.
One 、List Interface
1) characteristic :
Orderly 、 With subscript 、 Elements can be repeated .
2) A unique method of manipulating collections based on element indexes
List As Collection Sub interface of collection , Not only did they inherit Collection All methods in the interface , In addition, some operations based on element index are added
A unique way to make a collection , as follows :
- public void add(int index, E element) : Will specify the element , Add to the specified location in the collection .
- public E get(int index) : Returns the element at the specified location in the collection .
- public E remove(int index) : Remove the element at the specified location in the list , What is returned is the removed element .
- public E set(int index, E element) : Replace the element at the specified location in the collection with the specified element , Returns the pre update element of the value .
List Collection specific methods are all index related
Two 、ArrayList
1) characteristic
Slow addition and deletion of elements , Quick search , Because the most commonly used functions in daily development are
Query data 、 Traversal data , therefore ArrayList Is the most commonly used set .
2) Storage structure
Using array implementation .
- The reason of fast query :
An array is a continuous space , You can find the array through the first address , Through the index , You can find an element in the array . - The reason for the slow addition and deletion :
The length of the array is fixed , When we want to add or delete an array , You must create a new array , Copy the data of the original array . For example, the following delete operations :
We need to delete an element , A new array needs to be created , Length is original array -1, Copy other elements of the original array to the new array , Assign the address of the new array to the variable arr, The original array is destroyed , Garbage collection .
3) Advantages and disadvantages
advantage : Query data 、 Traversing data is very efficient . It uses indexes to quickly locate objects .
shortcoming : Is to insert or delete elements relative to LinkedList slower . Because the array is used , You need to move the following elements to adjust the order of the index .
4) Something to watch out for
In heap memory , Create and destroy arrays frequently , Copy the elements in the array , inefficiency , If there is a similar scene , You can consider LinkedList.
3、 ... and 、LinkedList
1) What is a linked list ?
A linked list is a physical storage unit that is discontinuous 、 Nonsequential storage structure
- Single chain list
- Double linked list
- Circular linked list
2) Storage structure
Chain table structure , Convenient elements add 、 Deleted sets , Access traversal is relative to ArrayList slower .
And it is a two-way linked list .
In actual development, the addition and deletion of a set element often involves the first and last operations , and LinkedList There are a lot of ways to start and end operations . These parties
Just let us know :
- public void addFirst(E e) : Inserts the specified element at the beginning of this list .
- public void addLast(E e) : Add the specified element to the end of this list .
- public E getFirst() : Returns the first element of this list .
- public E getLast() : Return the last element of this list .
- public E removeFirst() : Remove and return the first element of this list .
- public E removeLast() : Remove and return the last element of this list .
- public E pop() : Pop an element from the stack represented by this list .
- public void push(E e) : Push elements onto the stack represented by this list .
- public boolean isEmpty() : If the list does not contain elements , Then return to true.
边栏推荐
- Building Nacos 2 based on docker compose (using MySQL for persistence)
- Pychart encountered time zone problem when connecting to MySQL timezone
- MySQL built-in functions
- How slow is the application system on tongweb? How dead is it?
- OpenGL马赛克(八)
- Top slide immersive dialog
- Timeout thread log for tongweb
- Comment procéder à l'évaluation des algorithmes
- Compilation croisée helloworld avec cmake
- 【自动化测试】Cypress手册
猜你喜欢
Use the browser to cut the entire page (take chrome as an example)
Building a stand-alone version of Nacos series
Explanation of sentinel series' features, composition and deployment
Problems encountered in the use of PgSQL
MongoDB 多字段聚合Group by
How to Algorithm Evaluation Methods
MongoDB 多字段聚合Group by
Etcd understanding of microservice architecture
Solutions to conflicts between xampp and VMware port 443
Vagrant virtual machine installation, disk expansion and LAN access tutorial
随机推荐
Parallelgateway and exclusivegateway of 14 gateways
AUTOSAR actual combat tutorial pdf version
Basic operations of MySQL auto correlation query
计算两个时间相差的天数(支持跨月、跨年)
Current limiting and fusing of gateway gateway in Spirng cloud
JS output uincode code
No assessment summary
Explanation of service registration and discovery API of Nacos series
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
3. Postman easy to use
Jeffery0207 blog navigation
2 first experience of drools
How to Algorithm Evaluation Methods
9. Errorstartevent and errorboundaryevent of error events
Float type value range
@Detailed explanation of propertysource usage method and operation principle mechanism
Class conflicts caused by tongweb Enterprise Edition and embedded Edition
How to Algorithm Evaluation Methods
Set the correct width and height of the custom dialog
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file