The difference between ArrayList and Vector
Similarities:
1. ArrayList and Vector both inherit the same parent class and implement the same interface.
2. The bottom layer is implemented by arrays
3. The initial default length is 10.
Differences:
1. Synchronization:
Most public methods in Vector add synchronized keyword to ensure the synchronization of methods, that is, Vector thread-safe, and ArrayList thread is not safe.
2. Different expansion
Different internal properties may be the reason for different scaling methods.
ArrayList has two properties, the array elementData that stores data, and the size that stores the number of records.
Vector has three properties: the array elementData that stores data, the elementCount that stores the number of records, and the expansion factor capacityIncrement that expands the array size.
ArrayList extension method
//jdk1.8.0_91private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; 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); }It can be seen that when the expansion condition is met, the extended array size is 1.5 times the original array length and the larger of the passed parameters.
Vector extension method
//jdk1.8.0_91private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }It can be seen that when the expansion factor is greater than 0, the length of the new array is the original array length + the expansion factor, otherwise the length of the sub-new array is twice the length of the original array. Compare the new array length generated above with the passed parameter length, the larger one is the final new length.
Thank you for reading, I hope it can help you. Thank you for your support for this site!