What is the difference between ArrayList and LinkedList

One, the difference

  1. ArrayList is a data structure based on a dynamic array, and LinkedList is a data structure based on a doubly linked list (there are next and previous)
  2. ArrayList has get() and set() methods, random access is relatively fast (O(1)), and LinkedLsit access requires moving the pointer from the beginning (O(n)).
  3. For adding and deleting operations add() and remove operations, LinkedList is faster, because ArrayList needs to move data.
  4. But in some cases, LinkedList performs better than ArrayList, and some algorithms are more efficient when implemented in LinkedList. For example, when using the Collections.reverse method to reverse the list, its performance is better. LinkedList is also a better choice when you want to perform a large number of insert and delete operations on the list.
  5. Space waste: ArrayList wastes space mainly in the need to reserve a certain amount of space at the end of the list; the space cost of LinkedList1 is reflected in the larger space consumed by each element.

Two, summary

When the operation is to add data after a column of data instead of in the front or the middle, and you need to access the elements in it randomly, using ArrayList will provide better performance; when your operation is to add or When deleting data and accessing the elements in order, LinkedList should be used

Guess you like

Origin blog.csdn.net/Cxf2018/article/details/109323538