Read the principle of ArrayList


Read the principle of ArrayList

01. Principle

The bottom layer of ArrayList is implemented by array, which has the advantages and disadvantages of array. At the same time, it supports dynamic expansion (1.5 times of the original). So it is very suitable for the scene that needs to use index to access quickly. At the same time, due to its automatic expansion function, we need to pay attention to the need to specify the size when initializing the collection.

02. Features

  • It has continuous memory space and supports random access

Read the principle of ArrayList

  • Good modification performance

Read the principle of ArrayList

  • Poor performance of insert operation (memory copy is required when the insert position is not at the end of the line)

Read the principle of ArrayList

  • Poor deletion performance (memory copy is required when deletion location is not at the end). In practical applications, in order to improve the performance of data deletion, sometimes the data will be marked for digital deletion (which will cause more memory fragmentation), and when the internal memory is not enough, the data will be deleted and the memory will be copied, such as the mark tidy algorithm in GC. The specific process is as follows (you can also copy the memory first, and then delete the last bit of data)

Read the principle of ArrayList

##03 specific code

Finally, from the source specific analysis, ArrayList in the add (add), random access (get), delete (remove), insert (add), expansion operation (grow).


public boolean add(E e) {       
  Guarantee capacity internal (size + 1); // ensure that the array capacity is sufficient, otherwise, expand the growth       
  Elementdata [size + +] = E; // assign a value to the array [size], the current data length + 1? Return true;}

Random access (get):

public E get(int index) {       
   Rangecheck (index); // check whether the subscript value of the array exceeds the amount of data actually contained in the list 
   Return elementdata (index); // random access array   


public E remove(int index) {       
 Rangecheck (index); // check whether the subscript value of the array exceeds the amount of data actually contained in the list        
 Modcount + +; // used to determine whether the list has been operated during iterative traversal       
 E oldvalue = elementdata (index); // old value         
int numMoved = size - index - 1;        i
F (nummoved > 0) // the deletion position is not the end data, so the array is copied           
        System.arraycopy(elementData,index+1, elementData, index,  numMoved);
        Elementdata [-- size] = null; // a value is deleted, so the last data of the original array is null       
        return oldValue;   


public void add(int index, E element) {        
 ensureCapacityInternal(size + 1); // Increments modCount!!grow        
System.arraycopy (elementdata, index, elementdata, index + 1, size - index); // copy array       
 Elementdata [index] = element; // insert data     


private void grow(int minCapacity) {        // overflow-conscious code
 int oldCapacity = elementData.length;      
 Int new capacity = oldcapacity + (oldcapacity > > 1); // 1.5 times of the original new capacity
 if (newCapacity - minCapacity < 0)            
        newCapacity = minCapacity;        
 if (newCapacity - MAX_ARRAY_SIZE >0) 
   newCapacity =hugeCapacity(minCapacity);  // minCapacity is usually close tosize, so this is a win:        
elementData = Arrays.copyOf (elementdata, newcapacity); // copy the original data to the expanded array}