This article mainly studies the differences between ArrayList and LinkedList and the relevant contents of usage scenarios in Java programming. The details are as follows.
1. ArrayList is implemented based on an array, and its constructor is:
private transient Object[] elementData; private int size;
When ArryList is initialized, the elementData array size defaults to 10;
Every time 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 not safe, the Vector method is synchronous, thread-safe;
2. LinkedList is implemented based on double-linked lists:
Object element; Entry next, previous;
During initialization, there is a header Entry with the value null;
The advantage of using header is that there is a pre-entry entry and a post-entry entry in any entry (including the first and the last), so there is no special place to perform the insert operation at the beginning or end of the LinkedList object;
Use scenarios:
(1) If the application performs a large number of access or deletion operations on elements at each index position, the ArrayList object is far better than the LinkedList object;
(2) If the application mainly loops the list and inserts or deletes the operation during looping, the LinkedList object is far better than the ArrayList object.
Summarize
The above is all the content of this article about the difference between ArrayList and LinkedList and the analysis of usage scenario code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!