ArrayList inherits from the parent class AbstractList to implement the interface List, RandomAccess, Cloneable clone interface, Serializable serializable interface
Note the following three main variables, two static variables (existing in the JVM's Method Area method area), and an Object array type
Where EMPTY_ELEMENTDATA is an empty object array initialized when new ArrayList()
The initialization length is DEFAULT_CAPACITY value
Look at several main methods of arrayList
The addition method mainly calls the ensureCapacityInternal method. This method is mainly used to judge the length as follows. If the added object makes the length of the arrayList exceed the default of 10, the grow operation will be performed, which is the growth operation.
The growth operation is to copy the original array to the new array with the original length len*2+len and assign it back to the old array, which will die after a GC. Let's look at the get operation
Check if the index value is within the length if not throw indexoutofBoundsException array out of bounds exception
If the object value is obtained directly through the array subscript here, it will be returned.
-------------------------------------------------- --------------------- here is the dividing line
In view of the operation of remove at work, I just looked at this piece, or I am ready to write down my experience for emergencies
Let's discuss mainly remove(Object) and removeAll(Collections)
1. You can see that the main operation in remove is to traverse the array with subscripts and then call the Object.equals method. The source code of this method is as follows
What is compared is the address of the two objects. This involves the storage structure of objects in the java virtual machine. When data of two objects are exactly the same, the java virtual machine will not recreate an object but point the two variables to the same memory area
For example, String a="123", String b=new String("123"), a and b refer to the same address
2. After finding the same object, get its subscript index and call the fastRemove method to traverse the elements after index+1, move them all forward and set the last element to Null
The memory in this place will be freed after a Minor GC
removeAll(Collection)
Three lines of code are used in the source code to complete the reassignment of all the elements that do not exist and the parameter list C from the array elementDate 0 and assemble them into a new array. Here complement is false
Then start the traversal with w and assign the element to null. Note that the first IF is used to handle the contains exception. When an exception is thrown, r is not equal to size. The first if will be taken if an exception is not thrown. Go directly to the second IF
Summary: The above can read ArrayList collection is a data structure based on array implementation. The array element is Object
The growth mechanism is to create a new array that is 3 times as long as the original and copy the elements.
Attach the JVM virtual machine memory structure model document
https://blog.csdn.net/luomingkui1109/article/details/72820232