Differences between ArrayList and LinkedList and code analysis of usage scenarios

This paper mainly studies the differences between ArrayList and LinkedList in Java programming and the related contents of use scenarios. The details are as follows.

1. ArrayList is implemented based on array, and its constructor is:

When arrylist is initialized, the size of elementdata array is 10 by default; Every time you add (), first call ensurecapacity () to ensure that the array will not overflow. If it is full at this time, it will be expanded to 1.5 times + 1 of the array length, and then use the array.copy method to copy the original array to the new array; ArrayList thread is unsafe, and the vector method is synchronous and thread safe;

2. LinkedList is based on double linked list:

During initialization, there is a header entry with null value;

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

Usage scenario:

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

(2) if the application mainly circulates the list and inserts or deletes during the cycle, the LinkedList object is much better than the ArrayList object.

summary

The above is all about the differences between ArrayList and LinkedList and the code analysis of usage scenarios in this article. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>