Consolidate the foundation, read the next JDK source code, this article is to introduce the implementation of the next ArrayList.
1. ArrayList overview
The implementation of the variable size array of the List interface, located in the java.util.arraylist<e> of the API document. All optional list operations are implemented and all elements, including NULL, are allowed. In addition to implementing the list interface, this class provides methods to manipulate the size of the array used internally to store the list. (This class is roughly equivalent to the Vector class, except that this class is not synchronized.) )
2. ArrayList's inheritance System
Iterable: The interface declares an iterator.
Collection: The top-level interface of the collection framework, with two more important implementations, list and set.
Abstractcollection: Abstract class that implements some of the methods in the collection interface.
List: An ordered collection that allows users of this interface to precisely control where each element in the list is inserted. The user can access the element based on the integer index of the element (where it is located in the list) and search for the elements in the list.
Cloneable: Cloning interface, its implementation can be overridden by the Object.clone to achieve the cloning of objects.
Serializable: Class enables its serialization functionality by implementing the Java.io.Serializable interface. Classes that do not implement this interface will not be able to serialize or deserialize any of their states. All the subtypes of a serializable class are themselves serializable. The serialized interface has no methods or fields and is used only to identify the semantics of serializable.
Abstractlist: Inherited the Abstractcollection, overriding some of the methods. This class provides the List backbone implementation of the interface to minimize the work required to implement the interface that is supported by the random Access data store (such as arrays).
Randomaccess: The tag interface used by the List implementation to indicate that it supports a fast (usually fixed-time) random access. The primary purpose of this interface is to allow a generic algorithm to change its behavior so that it provides good performance when applied to random or contiguous access lists.
3. Implementation of ArrayList:
Member variables
Private transient Object[] Elementdata; // The underlying uses array implementations Private int size; // number of elements in ArrayList Private Static Final int Default_capacity = 10; // The default growth factor Private Static Final Object[] Empty_elementdata = {}; // reserved Empty array
constructor function
1 /**2 * Specify the initial length of the list3 * @paraminitialcapacity Initial length4 */5 PublicArrayList (intinitialcapacity) {6 if(Initialcapacity > 0) {//creates an object array of the specified length if the initial length is >07 This. Elementdata =Newobject[initialcapacity];8}Else if(initialcapacity = = 0) {//if the given initial length = 0, point to an empty object array9 This. Elementdata =Empty_elementdata;Ten}Else{//throws an exception if the given initial length is <0, because a negative-length array cannot be created One Throw NewIllegalArgumentException ("Illegal capacity:" + A initialcapacity); - } - } the - /** - * Default length of list creation - */ + PublicArrayList () { - //the underlying array points to the default empty object array + This. Elementdata =Defaultcapacity_empty_elementdata; A } at - /** - * Create a new ArrayList and copy elements from an object that implements the collection interface into our new ArrayList - * @paramC - */ - PublicArrayList (collection<?extendsE>c) { in //converts the passed-in object to an array, and elementdata points to it -Elementdata =C.toarray (); to //if the length of C is 0, point to the reserved empty array, if the length is not 0, and the C object's generic is not an object, the data is copied into a new array by means of the static method arrays.copyof () + if((size = elementdata.length)! = 0) { - if(Elementdata.getclass ()! = object[].class) theElementdata = arrays.copyof (elementdata, size, object[].class); *}Else { $ This. Elementdata =Empty_elementdata;Panax Notoginseng } -}
ArrayList Source Code Analysis