I. ArrayList constructor and construction method
In ArrayList, two empty arrays are defined, respectively, to the array to which the default constructor method should be specified, when the array already has a given capacity, but the capacity equals 0. In addition, when the collection object is passed into the constructor, the ToArray method of the object is called directly, and point the reference inside the container to the resulting array. The source code is as follows:
Private Static Final LongSerialversionuid = 8683452581122892189L; /*** The capacity of the default array*/ Private Static Final intDefault_capacity = 10; /*** An array of empty objects given in the default constructor*/ Private Static Finalobject[] Empty_elementdata = {}; /**In a constructor with parameters, if the argument is 0, use this empty object array*/ Private Static Finalobject[] Defaultcapacity_empty_elementdata = {}; /**array of loaded elements*/ transientObject[] Elementdata;//Non-private to simplify nested class access /*** Container Size * *@serial */ Private intsize; /*** If the specified capacity >0.new an object array if ==0. Use a reference with a specified capacity of 0 to point to*/ PublicArrayList (intinitialcapacity) { if(Initialcapacity > 0) { This. Elementdata =NewObject[initialcapacity]; } Else if(initialcapacity = = 0) { This. Elementdata =Empty_elementdata; } Else { Throw NewIllegalArgumentException ("Illegal capacity:" +initialcapacity); } } PublicArrayList () { This. Elementdata =Defaultcapacity_empty_elementdata; } PublicArrayList (collection<?extendsE>c) {Elementdata=C.toarray (); if((size = elementdata.length)! = 0) { //The ToArray method may not return an object array if(Elementdata.getclass ()! = object[].class) Elementdata= arrays.copyof (elementdata, size, object[].class); } Else { //point the reference to an empty array. This. Elementdata =Empty_elementdata; } }
Two. Dynamic sizing (TrimToSize method)
This method causes the array inside the container to be the same length as the number of its stored elements, by invoking the Arrays.copyof method (which is used extensively in the implementation of ArrayList), and completes the reference variable inside the container to a new array of length as the number of elements in the container.
Public void trimtosize () { modcount+ +; operations that are modified for the capacity of the container, modcount+1 if (Size < elementdata.length) {= (size = = 0 ) ? empty_elementdata : arrays.copyof (elementdata, size); } }
Three. Ensurecapacity method
This method increases the capacity of the ArrayList instance to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter. It is important to note that when an array reference is directed to an empty array of default constructors, it will determine whether to increase the capacity <10, or, if less than 10, increase the length of the array directly to 10. When the length of the array is insufficient to accommodate the element, the length of the array is changed to 1.5 times times the original. The code is as follows:
Public voidEnsurecapacity (intmincapacity) { intMinexpand = (Elementdata! =defaultcapacity_empty_elementdata)//if it is not the default constructor, the lower bound is 0? 0//if it is the default constructor. So the lower limit is ten.//no expansion required for a given capacity of less than 10: default_capacity; if(Mincapacity >Minexpand) {ensureexplicitcapacity (mincapacity); } } Private voidEnsurecapacityinternal (intmincapacity) { if(Elementdata = =defaultcapacity_empty_elementdata) {mincapacity=Math.max (default_capacity, mincapacity); } ensureexplicitcapacity (mincapacity); } Private voidEnsureexplicitcapacity (intmincapacity) {Modcount++; //overflow-conscious Code if(Mincapacity-elementdata.length > 0) grow (mincapacity); } Private Static Final intMax_array_size = integer.max_value-8; Private voidGrowintmincapacity) { //overflow-conscious Code intOldcapacity =elementdata.length; intNewcapacity = oldcapacity + (oldcapacity >> 1); if(Newcapacity-mincapacity < 0)//1.5 times times the old capacity of <mincapacity, only use mincapacityNewcapacity =mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); //Elementdata point to a copy-good arrayElementdata =arrays.copyof (Elementdata, newcapacity); } Private Static intHugecapacity (intmincapacity) { if(Mincapacity < 0)//Overflow Throw NewOutOfMemoryError (); return(Mincapacity > Max_array_size)?Integer.MAX_VALUE:MAX_ARRAY_SIZE; }
Four. IndexOf method
Since the bottom of the ArrayList method is an array implementation, the Abstractlist IndexOf method provides a replication, and the underlying array is used to implement the method.
Public intindexOf (Object o) {if(O = =NULL) { for(inti = 0; i < size; i++) if(elementdata[i]==NULL) returni; } Else { for(inti = 0; i < size; i++) if(O.equals (Elementdata[i]))returni; } return-1; }
Five. Add method
Ensurecapacity guarantees that if it is a default constructor, then an array of 10 capacity is created, and if not, the array will be enlarged when it is full. (The capacity of the new array is typically 1.5 times times the size of the old array).
Public Boolean Add (e e) { + 1); // increase modcount and capacity ~ elementdata[size++] = e; return true ; }
Six. Add (int index,e Element) method
This method uses the System.arraycopy method to copy the index position and all subsequent elements to the location of the index+1, and adds 1 to the length of the array. The value is then assigned at the index.
Public void Add (int Index, E Element) { Rangecheckforadd (index); + 1); System.arraycopy (Elementdata, index, Elementdata, index + 1, - index); = element; Size+ +; }
Seven. AddAll method.
By converting the collection collection to an array, the System.arraycopy method is called to complete the increase for the arrays.
Public Boolean extends E> c) { = c.toarray (); int numnew = a.length; + numnew); // increase the value of the Modcount System.arraycopy (A, 0, elementdata, size, numnew); + = numnew ; return Numnew! = 0; }
Eight. RemoveAll and Retainall methods
The RemoveAll and Retainall methods implement removing elements by passing different parameters to the same method, and the method directly records the same or different elements of the given collection object in the Array object.
Public BooleanRemoveAll (collection<?>c) {objects.requirenonnull (c); returnBatchremove (c,false); } Public BooleanRetainall (collection<?>c) {objects.requirenonnull (c); returnBatchremove (c,true); }//The implementation of the RemoveAll and Retainall methods is performed in this method: Private BooleanBatchremove (collection<?> C,Booleancomplement) { Finalobject[] Elementdata = This. Elementdata;//establishes a reference to an array type pointing to an in-memory array object intR = 0, w = 0;//R iterates over the index of the array. W records the same element or different elements BooleanModified =false; Try { for(; r < size; r++) if(C.contains (elementdata[r]) = =complement) Elementdata[w++] =Elementdata[r]; } finally { if(r! =size) {system.arraycopy (Elementdata, R, Elementdata, W, Size-R); W+ = Size-R; } if(W! =size) { //clear to let GC do it work for(inti = W; i < size; i++) Elementdata[i]=NULL;//The same or different elements are recorded before the W indexModcount + = size-W; Size=W; Modified=true; } } returnModified
Java Source Beginner _arraylist