An analysis of ArrayList will start with the Add and remove methods only.
ArrayList class definition:
Public class extends Implements List<e>
ArrayList Basic Properties:
/*** Default initial capacity. (Defaults to the size of the initialization)*/ Private Static Final intDefault_capacity = 10; /*** Shared Empty array instance used for empty instances. * An empty array, which is given an empty array by default when the parameterless constructor is called*/ Private Static Finalobject[] Empty_elementdata = {}; /*** An array of truly stored data*/ Private transientobject[] Elementdata; /*** The size of the ArrayList (the number of elements it contains). * The number of data stored*/
View Code
ArrayList The Add method:
/*** Appends the specified element to the end of this list. * Insert data, each time it is inserted from the end*/ Public BooleanAdd (e e) {ensurecapacityinternal (size+ 1);//increments modcount!!elementdata[size++] =e; return true; }Private voidEnsurecapacityinternal (intmincapacity) { if(Elementdata = = Empty_elementdata) {//If an empty array is represented hereMincapacity =Math.max (default_capacity, mincapacity); } ensureexplicitcapacity (mincapacity); } Private voidEnsureexplicitcapacity (intmincapacity) {Modcount++;//JDK Source Reading Notes Java Collection Framework (iii) (MODCOUNT) if(Mincapacity-elementdata.length > 0) /*** mincapacity=size+1 * Only used when the array actually stores the number of elements +1 is greater than the array length is, needs to be enlarged*/grow (mincapacity); } /*** Increases the capacity to ensure so it can hold on least the * number of elements specified by the Minimu M capacity argument. * Increase the capacity of the array to ensure that at least minimum amount of data can be stored*/ Private voidGrowintmincapacity) { intOldcapacity =elementdata.length; //A signed right shift is equivalent to dividing by 2, so after each expansion, the array is 1.5 times times the length of the original. intNewcapacity = oldcapacity + (oldcapacity >> 1); if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); //because of the new array generated after the expansion, here is the transfer of data between the old and new arraysElementdata =arrays.copyof (Elementdata, newcapacity); }
ArrayList the Remove method:
The Remove method is divided into remove (Object O) and remove (int index). The two methods are similar, so either one is selected for analysis.
/*** Remove the element that specifies the subscript*/ PublicE Remove (intindex) {Rangecheck (index);//Subscript out of bounds checkModcount++;//"http://www.cnblogs.com/jw93/p/6845825.html"Detailed IntroductionE OldValue =Elementdata (index); /*** After removing an element, you need to move the element following the element forward * nummoved indicates the number of elements that need to be moved*/ intnummoved = size-index-1; if(nummoved > 0) system.arraycopy (elementdata, index+1, Elementdata, Index, nummoved);//here is the native methodElementdata[--size] =NULL;//clear to let GC do it work returnOldValue; } /*** Subscript cross-border check*/ Private voidRangecheck (intindex) { if(Index >=size)Throw NewIndexoutofboundsexception (outofboundsmsg (index));//throw array subscript out of bounds } /*** Details when throwing exceptions*/ PrivateString Outofboundsmsg (intindex) { return"Index:" +index+ ", Size:" +size; }
JDK Source Reading Notes Java Collection framework (ii) (ArrayList)