Java basics and java Basics
A set includes commonly used ArrayList, HashMap, and HashSet, as well as uncommonly used stacks and Queue, thread-safe Vector and HashTable, thread-unsafe failover list, And TreeMap.
ArrayList is a dynamic array that implements the List interface. The initial capacity is 10, that is, the array size. Each time an element is added to the container, the capacity check is performed. When the overflow is fast, the resizing operation is performed (data is re-copied to the new array ). It is not synchronous and used in multiple threads. It is best to ensure synchronization during creation.
For example:
List list = Collections.synchronizedList(new ArrayList());
Create: Create a transient modified array.
New:
Method: add (E), add (int index, e element), addAll (Collection <? Extends E> c), addAll (int index, Collection <? Extends E> c), set (int index, E element)
Generally, the container size is checked first to check whether expansion is required. Call the ensureCapacity () method to resize the ArrayList set.
Public void ensureCapacity (int minCapacity) {// modify the timer modCount ++; // ArrayList capacity size int oldCapacity = elementData. length;/** if the length required is greater than the length of the current array, perform the expansion operation */if (minCapacity> oldCapacity) {Object oldData [] = elementData; // calculate the new capacity, which is 1.5 times the current capacity. int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity <minCapacity) newCapacity = minCapacity; // copy the array to generate the new array elementData = Arrays. copyOf (elementData, newCapacity );}}
If you insert an element add (int index, E element) at the specified index position, System is called. the arraycopy () method is used to leave the index position blank for new data insertion. Here, the right shift of array data is required, which is very troublesome and time consuming, therefore, if the specified data set requires a large number of insert operations, we recommend that you use the sequence list.
Delete:
Method: remove (int index), remove (Object o), removeRange (int fromIndex, int toIndex), removeAll ()
After the element is deleted, the System. arraycopy (...) method is called to adjust the position of the remaining element.
Obtain:
Method: get (int index)
Because ArrayList is a dynamic array, we can obtain the elements in ArrayList Based on the subscript, and the speed is relatively fast. Therefore, ArrayList is suitable for random access.
The random List is a linked List that implements the List interface. It is superior to ArrayList when inserting or deleting data, while random access is inferior to ArrayList.