ArrayList related interview questions

The difference between ArrayList and LinkedList

  1. Thread safety: neither guarantees thread safety. If you need thread safety, you can use:

Thread-safe solution:
Method 1: Collections.synchronizedList(new LinkedList())
Method 2: LinkedList and ArrayList are replaced with thread-safe collections, such as CopyOnWriteArrayList, ConcurrentLinkedQueue...
Method 3: Vector (the synchronized keyword is used internally to achieve synchronization)

  1. The underlying data structure: The underlying data structure of ArrayList is an Object array. LinkedList is a doubly linked list (1.6 and before use a circular linked list).
  2. The time complexity of insert and delete operations is different:
    The bottom layer of ArrayList is array storage. The time complexity of inserting and deleting elements is affected by the position of the elements. If not specified, the default position is added to the tail of the linked list, is O (. 1), if it is added to a specified position i compared to O (Ni)
    the LinkedList bottom of the list is, in the complex time of inserting and removing elements from element position influence approximated O (1), it is O(n) if inserted into the specified position.
  3. Whether to support fast access: LinkedList does not support, ArrayList supports, because the bottom layer is an array.
  4. Memory footprint: ArrayList's memory footprint is smaller than LinkedList under the same circumstances. The memory usage of ArrayList is mainly when no data is stored in the tail. Each element of the LinkedList memory needs to store the predecessor node and the successor node additionally.

RandomAccess interface

  1. The RandomAccess interface is not defined. Just as an identifier. Identifies that the class that implements this interface has the function of random access.
  2. For example, in the binarySearch() method, it is used to determine whether the incoming list is an instance of RandomAccess, if it is, call the indexedBinarySearch() method, if not, then call the iteratorBinarySearch() method.
  3. The list of RandomAccess is implemented, and the first way to traverse is for loop, then foreach (foreach bottom layer is implemented by iterator). Iterator is preferred if it is not implemented.
  4. For ArrayList, this interface is implemented but LinkedList is not. Because the bottom layer of ArrayList is an array, the array naturally supports random access, and the time complexity is O(1). The bottom layer of LinkedList is that the linked list cannot be accessed randomly and can only be traversed from the head pointer.

The difference between ArrayList and Vector, why use ArrayList instead of Vector

  1. All methods of the Vector class are synchronized. If it is only operated by one thread, it will consume a lot of time in synchronization operations.
  2. ArrayList is not synchronized, so it is better to use ArrayList without thread safety.

ArrayList expansion mechanism

ArrayList has three construction methods
. The first: the default construction method without parameters.
public ArrayList()
The second: the constructor with the initial capacity parameter (the user can specify the capacity by himself). The
public ArrayList(int initialCapacity)
third: construct a list containing the specified collection elements.
public ArrayList(Collection<? extends E> c)

  1. When using the parameterless construction, an empty array is initialized. The real capacity is allocated to the array when the elements are actually added. Even when the add(E e) method is used, the array capacity will be expanded to 10.

The specific process: When we want to add the first element to the ArrayList, elementData.length is 0 (because it is still an empty list), because the ensureCapacityInternal() method is executed, minCapacity is now 10. At this time, minCapacity-elementData.length> 0 is established, so it will enter the grow(minCapacity) method.
When adding the second element, minCapacity is 2. At this time, elementData.length (capacity) is expanded to 10 after adding the first element. At this time, minCapacity-elementData.length> 0 does not hold, so it will not enter (execute) the grow(minCapacity) method.
When adding the 3rd, 4th... to the 10th element, the grow method is still not executed, and the array capacity is 10.
Until the 11th element is added, minCapacity (which is 11) is greater than elementData.length (which is 10). Enter the grow method to expand.
After each expansion of ArrayList, the capacity will be about 1.5 times of the original (oldCapacity is 1.5 times for even numbers, otherwise it is about 1.5 times)! The parity is different, for example: 10+10/2 = 15, 33+33/2=49. If it is an odd number, the decimal will be dropped.

It is best to use the ensureCapacity method before adding a large number of elements to reduce the number of incremental reallocations.
More detailed source code interpretation

Reference: JavaGuide

Guess you like

Origin blog.csdn.net/H1517043456/article/details/107535721