java -- ArrayList

Java -- ArrayList

 

 

       ArrayList is implemented based on arrays. It is a dynamic array whose capacity can be automatically increased, similar to the dynamic application of memory in C language, and the dynamic increase of memory.

 

 

 

advantage

 

  • Find data performance is fast, faster than Vector
  • Any data type can be inserted
  • It is implemented as an array, which saves space, but has a capacity limit. The default initialization size is 10. When it exceeds, it will automatically expand to 1/2 (50%) of the original capacity, that is, the automatic expansion mechanism.

 

 

shortcoming

 

  • Not thread safe
  • Update and delete performance will be poor, because the essence is an array
  • Automatic growth will result in a new copy of the data to the new array, so if you can predict the amount of data, you can specify its capacity when constructing the ArrayList. Applications can also use the ensureCapacity operation to increase the capacity of an ArrayList instance before adding a large number of elements, which can reduce the number of incremental reallocations.
  • Requires contiguous storage space

 

 

working principle

 

The bottom layer is implemented using an array

 

private transient Object[] elementData;

 

 

 

Construction method

 

    ArrayList provides three types of constructors

 

  • Constructs an empty list with a default initial capacity of 10
  • Constructs an empty list specifying the initial capacity
  • Constructs a list containing the elements of the specified collection, in the order that this collection's iterator returns them.

 

public ArrayList() {  
    this(10);  
}  
  
public ArrayList(int initialCapacity) {  
    super();  
    if (initialCapacity < 0)  
        throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);  
    this.elementData = new Object[initialCapacity];  
}  
  
public ArrayList(Collection<? extends E> c) {  
    elementData = c.toArray();  
    size = elementData.length;  
    // c.toArray might (incorrectly) not return Object[] (see 6260652)  
    if (elementData.getClass() != Object[].class)  
        elementData = Arrays.copyOf(elementData, size, Object[].class);  
}

 

 

 

 

Insert, modify, store data

 

  • Find data and modify the specified data get(index)/set(index,e), high performance
  • Adding an add(e) operation also performs well (adding elements directly at the end of the array)
  • Insert elements or delete operations according to the subscript, add(index,e)/remove(index)/remove(e), the performance will be low, you need to use system.arraycopy() to move the affected elements



 

 

 

Adjust capacity

 

      Whenever an element is added to the array, it is necessary to check whether the number of added elements exceeds the length of the current array. If it exceeds, the array will be expanded to meet the needs of adding data.

 

Expansion rules

 

  1. Copy the elements from the old array to the new array
  2. Each time the array capacity grows by about 1.5 times its original capacity

 

   It can be known that the cost of such expansion is too high, so it should be dealt with appropriately.

 

 

Solution

 

  1. When we can predict the number of elements to be saved, when constructing an ArrayList instance, specify its capacity to avoid the occurrence of array expansion.
  2. According to actual needs, manually increase the capacity of the ArrayList instance by calling the ensureCapacity method.

 

 

 

Concurrent processing

 

      ArrayList also uses a fail-fast mechanism, which is achieved by recording the modCount parameter. Rather than risking arbitrary nondeterministic behavior at some indeterminate time in the future, iterators can fail completely quickly in the face of concurrent modifications.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037360&siteId=291194637