ArrayList

ArrayList


Source code analysis is reproduced from (already detailed): http://blog.csdn.net/u010887744/article/details/49496093

ArrayList common interview questions are reproduced from: https://www.cnblogs.com/softidea/p/6410680 .html

Summary:
ArrayList is implemented based on dynamic arrays. It is suitable for query, because there are subscripts, it is not suitable for addition and deletion, because the array will be copied, and then the System.arrayCopy( ) method will be called, which consumes resources.
Therefore: ArrayList is more suitable when query operations on data are more than addition and deletion operations.

Question1: How is it based on dynamic arrays?
        The add operation of ArrayList first checks the array to determine whether the number + 1 in the array is greater than its default value of 10. If it is not greater than 10, the expansion operation will not be performed. +1 for the expansion operation, and then the bottom layer calls System.arrayCopy( ) to copy the original data to the new data for expansion.
The remove operation will make -1 in the data, and the capacity is greater than 10, and will perform a shrinking operation. Call trimToSize( ) to restore it to 10, easy = 10, and this operation will not be performed.
Note: System.arrayCopy( ) will consume resources in particular, so ArrayList is not suitable for adding and deleting operations.

Question2: The expansion mechanism of ArrayList?
ArrayList list = new ArrayList( );
When creating a collection in this way, the created capacity is 0 by default. In jdk1.8, when adding the collection for the first time, the default capacity will be assigned to the size of the collection.
Expansion mechanism: When adding elements to the collection, the size of the internal array will be checked. If it is greater than 10, it will be expanded. The source code of the expansion:
        private void grow(int minCapacity) {
            int oldCapacity = elementData.length;
            // Here is the expansion: the original capacity of the collection + the right shift of the original capacity by 1 bit.
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
        }

Note: For the right shift operation, if the original value is 10 (1*2^1=2 in decimal), the right shift becomes 01 (1*2^0=1 in decimal), that is, a right shift means / 2. Computers do calculations on binary.

Therefore, the mechanism in the grow method is: size after expansion = size before expansion + size before expansion/2 = 1.5 size before expansion.

An example of the internal expansion mechanism of ArrayList:
if we insert 17 objects continuously, it will expand twice, the first time is when the 11th object is inserted (expanded to size = 10*1.5 = 15), and the second time is when the 11th object is inserted. Expand when the 16th object is inserted (expanded to size= 15*1.5+1 = 22.5).

This blog, interspersed with Vector implementation classes.
Comparison of Vector and ArrayList:
1: The methods of Vector are all synchronized and thread-safe, while the methods of ArrayList are not. Because the synchronization of threads will inevitably affect the performance, the performance of ArrayList is better than that of Vector. Okay. 
2: When the elements in Vector or ArrayList exceed its initial size, Vector will double its capacity (2 times), while ArrayList will only increase its size by 50% (1.5 times), so ArrayList will help save memory space .

Note: ArrayList internal expansion source code:
int newCapacity = oldCapacity + (oldCapacity >> 1);--- expand the capacity by 1.5 times.

Vector internal expansion source code:
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?capacityIncrement : oldCapacity);----capacityIncrement : growth coefficient, when growing, capacityIncrement is 0, expanding twice the capacity


Note: Vector is generally not used now, because its internal methods are all synchronized modified synchronization methods, which will cause multi-thread blocking problems and affect performance. If the collection (CopyOnWriteArrayList and ArrayList) is created inside the method, use ArrayList, because multithreading will not affect the data in local variables; if the collection is used as a member variable, jdk1 can be used in the case of multithreading The CopyOnWriteArrayList concurrent collection class in the java.concurrent package in .5 is processed. So, to sum up, Vector is not applicable.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326146103&siteId=291194637
Recommended