ArrayList and LinkedList differences and use scenario code parsing

  • 2021-01-06 00:34:58
  • OfStack

This paper mainly studies the difference between ArrayList and LinkedList in Java programming and the relevant content of the use scenario. The specific introduction is as follows.

ArrayList is an array based implementation. Its constructor is:


private transient Object[] elementData; 
private int size; 

When ArryList is initialized, the default size of the elementData array is 10;
ensureCapacity () is called each time to ensure that the array does not overflow. If the array is full at this time, it will expand to 1.5 times +1 of the array length.
ArrayList is thread-safe. Vector methods are synchronized and thread-safe.

2, LinkedList is based on double linked list implementation:


Object element; 
Entry next, 
   previous; 

When initialized, there is an header Entry with the value null;

The advantage of using header is that there is a pre-entry and a post-entry for every entry (including the first and last), so there is no special place to insert at the beginning or end of an LinkedList object;

Usage Scenario:

(1) The ArrayList object is much better than the LinkedList object if the application performs a large number of access or delete operations on the elements at each index position;

(2) If the application is primarily looping through lists and doing inserts or deletes while looping, the LinkedList object is much better than the ArrayList object.

conclusion

The above is this article about ArrayList and LinkedList differences and the use of the scene code analysis of all content, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: