| title | category | tag | |
|---|---|---|---|
ArrayList Source Code Analysis |
Java |
|
The underlying structure of ArrayList is an array queue, which is equivalent to a dynamic array. Compared to arrays in Java, its capacity can grow dynamically. Before adding a large number of elements, applications can use the ensureCapacity operation to increase the capacity of the ArrayList instance. This can reduce the number of incremental reallocations.
ArrayList inherits from AbstractList and implements the interfaces List, RandomAccess, Cloneable, and java.io.Serializable.
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
}List: Indicates that it is a list that supports operations such as adding, deleting, and searching, and can be accessed by index.RandomAccess: This is a marker interface indicating that theListcollection implementing this interface supports fast random access. InArrayList, we can quickly retrieve an element object by its index, which is fast random access.Cloneable: Indicates that it has the ability to be copied, allowing for deep or shallow copy operations.Serializable: Indicates that it can be serialized, meaning it can convert objects into byte streams for persistent storage or network transmission, which is very convenient.
ArrayListis the main implementation class ofList, usingObject[]for storage, suitable for frequent lookup operations, and is not thread-safe.Vectoris an older implementation class ofList, also usingObject[]for storage, and is thread-safe.
ArrayList can store any type of object, including null values. However, it is not recommended to add null values to ArrayList, as null values are meaningless and can make the code difficult to maintain. For example, forgetting to handle null checks can lead to a NullPointerException.
Example code:
ArrayList<String> listOfStrings = new ArrayList<>();
listOfStrings.add(null);
listOfStrings.add("java");
System.out.println(listOfStrings);Output:
[null, java]
- Thread Safety: Both
ArrayListandLinkedListare unsynchronized, meaning they do not guarantee thread safety. - Underlying Data Structure:
ArrayListuses anObjectarray for storage;LinkedListuses a doubly linked list data structure (before JDK1.6 it was a circular linked list, which was removed in JDK1.7. Note the difference between a doubly linked list and a doubly circular linked list, which is introduced below!). - Impact of Element Position on Insertion and Deletion:
ArrayListuses an array for storage, so the time complexity for inserting and deleting elements is affected by the position of the elements. For example, when executing theadd(E e)method,ArrayListdefaults to appending the specified element to the end of the list, which has a time complexity of O(1). However, if you want to insert or delete an element at a specified positioni(add(int index, E element)), the time complexity becomes O(n) because the elements from indexiand the subsequent (n-i) elements need to be shifted.LinkedListuses a linked list for storage, so inserting or deleting elements at the head or tail is not affected by the position of the elements (add(E e),addFirst(E e),addLast(E e),removeFirst(),removeLast()), with a time complexity of O(1). However, if you want to insert or delete elements at a specified positioni(add(int index, E element),remove(Object o),remove(int index)), the time complexity is O(n) because you need to move to the specified position first before inserting or deleting.
- Support for Fast Random Access:
LinkedListdoes not support efficient random access to elements, whileArrayList(which implements theRandomAccessinterface) does. Fast random access means quickly retrieving an element object by its index (corresponding to theget(int index)method). - Memory Space Usage: The space waste of
ArrayListmainly lies in reserving a certain capacity at the end of the list,
