ArrayList and LinkedList compare
in use List When , It is often used ArrayList and LinkedList, But when should it be used ArrayList, When to use LinkedList, Here is a brief explanation .
ArrayList principle
ArrayList It actually corresponds to an array of data structures (Array).
ArrayList The data storage structure of is an array . Arrays are characterized by :
* Occupied space fixed , But when there's not much data , Waste of space ;
* When inserting data , Because the space size is fixed , So there's no need to allocate space for the data , Just insert the data . But when inserting , The data behind needs to be moved . Slower ;
* When deleting data , It is necessary to move the data behind the space . Slower ;
* When getting the specified location data , You can use the subscript of the data to locate directly . Fast ;
LinkedList principle
LinkedList In fact, it corresponds to the linked list in the data structure .
LinkedList The data storage structure of is linked list . In particular, linked lists :
* The space occupied is not fixed , Dynamic allocation with data size , No waste of space ;
* When inserting data , Need to allocate space dynamically , And then insert it . The latter data does not need to be moved . Fast ;
* When deleting data , Delete directly , And free up space . The latter data does not need to be moved . Fast ;
* When getting the specified location data , Unable to locate directly , Need to find one by one , Until the corresponding data is found , Slow ;
ArrayList and LinkedList compare
According to the above face ArrayList and LinkedList Explanation of principle , The summary is as follows .
ArrayList and LinkedList Comparison of characteristics
ArrayList LinkedList
Insert operation Slow
Delete operation Slow
Get the specified location data Fast
ArrayList and LinkedList purpose
If the operation focuses on inserting , delete , Then use it LinkedList bar , It's fast ;
If the operation is focused on getting the specified location data , Then use it ArrayList, It's fast ;
notes : The operation to get the data of the specified location , At present, we can find it , Ergodic, etc .
notes : About what is an array (Array), What is a linked list . Let's take a look at the data structure , Or Baidu , Self invigorating brain . ha-ha
Technology
Daily Recommendation