java ArrayList collection in List Detailed

List is a collection class that implements the interface

List:

Features: orderly, repeatable

It has two common implementation class:

一。ArrayList:

Features: carried out in the form of an array of storage, and therefore faster random access, it applies to all queries.

Disadvantages: not applicable to the insert and delete operations are required because each moving element in the array.

According to the source we can draw the following points:

1.ArrayList at initialization time if we do not have the length specified, it will have a default length of 10,

private static final int DEFAULT_CAPACITY = 10;

 

2. If we add a new element of time than the original capacity, the ArrayList is how to do it?

This involves the expansion mechanism ArrayList, since ArrayList is stored in an array, then certainly inherited the characteristics of the array can not be changed once declared, since it can not be changed, that java is how to solve this issue?

transient Object[] elementData;

This is a temporary variable array declaration beginning to prepare for the future expansion of the array

public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

ensureCapacityInternal upper code (size + 1) is the beginning of expansion

We continue to thoroughly point into the source code

private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }

    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

We found ensureCapacityInternal see this in depth we continue to call ensureExplicitCapacity

 

 
 
private static final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; // default empty set
 
 
private static final int DEFAULT_CAPACITY = 10;//默认10

Private
static int calculateCapacity (Object [] elementData of, int be minCapacity) {
      // if the current elemenrData (current data) is an empty set, the next step of acquiring a capacity expansion
IF (== elementData of DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { return Math.max (DEFAULT_CAPACITY, be minCapacity); } return be minCapacity; }

Then we return to the previous

Private  void ensureExplicitCapacity ( int be minCapacity) { 
        ModCount ++ ; // number of times each modification set ( values in AbstractList.class)
// overflow-Conscious code detecting overflow 
// If the minimum required capacity of> length of the array, it is necessary expansion
IF (be minCapacity - elementData.length> 0 ) Grow (be minCapacity);}
 Private  void Grow ( int be minCapacity) {
         // overflow-Conscious code 
        int oldCapacity = elementData.length;
         int newCapacity oldCapacity + = (oldCapacity >>. 1 ); // 1.5 (1.8,1.7) of the initial capacity (1.5 times 1.6 + 1'd)
         IF (newCapacity - be minCapacity <0 ) 
            newCapacity = be minCapacity; // if it is not enough to put the assignment need value
         IF (newCapacity - MAX_ARRAY_SIZE> 0 ) 
            newCapacity = hugeCapacity (be minCapacity); // Analyzing large capacity, the following code
         / / minCapacity IS usually use Close to size, SO IS A win the this:
        = elementData of Arrays.copyOf (elementData of, newCapacity); // This is the reason why an array may also be stored in the form of expansion 
    } 

    Private  static  int hugeCapacity ( int be minCapacity) {
         IF (be minCapacity <0) // overflow memory overflow 
            the throw  new new an OutOfMemoryError ( );
         return (be minCapacity> MAX_ARRAY_SIZE) // ternary operator? 
            Integer.MAX_VALUE: 
            MAX_ARRAY_SIZE; 
    }

After reading the above code we can get out if you do not give the initial value, the default value is 10, did not change when expansion in the original array, but copy an array of 1.5-fold increase

3.ArrayList is thread safe. If you want to implement a thread-safe can use the synchronized keyword or use Collections.synchronizedList () method is as follows:

 List<Object> objects = Collections.synchronizedList(new ArrayList<>());

 

Guess you like

Origin www.cnblogs.com/guochenchen/p/11204633.html