ArrayList Overview
ArrayList is the most common implementation class of the list interface, the underlying is implemented by the array, can be stored in null values, each ArrayList has a capacity (capacity) attribute, the initial value is 10, representing the storage space of the underlying array, ArrayList automatically completes capacity amplification when the number of current elements in the container is greater than the array storage capacity, so we do not have to consider capacity when adding elements to ArrayList, and the container has already helped us with the capacity expansion process.
ArrayList has public ArrayList (), public ArrayList (int initialcapacity), public ArrayList (collection<? extends E > C) Three constructors, you can specify capacity when creating ArrayList or choose to assign values to the current ArrayList with other collection ( implemented in the underlying array copy ) or create an array of default capacity with the parameterless constructor, When size is less than the current number of elements, capacity expansion is completed automatically.
analysis of common methods
Add method
//adding elements to the end of an array Public BooleanAdd (e e) {ensurecapacityinternal (size+ 1);//capacity self-incrementelementdata[size++] =e; return true; }//add an element to the set position (index) Public voidAddintindex, E Element) {Rangecheckforadd (index);//Check CapacityEnsurecapacityinternal (size + 1);//capacity self-incrementSystem.arraycopy (Elementdata, index, elementdata, index + 1,size-index);//move backwards from the index+1 elementElementdata[index] = element;//Assigning a value to a newly added elementsize++; }//The insertion complexity and position are related to the number of moving elements, and this method has linear time complexity.
AddAll method
//add multiple elements to the tail of an array Public BooleanAddAll (collection<?extendsE>c) {object[] a= C.toarray ();//to add a collection into an array of objects intNumnew =a.length; Ensurecapacityinternal (Size+ numnew);//Expansion CapacitySystem.arraycopy (A, 0, elementdata, size, numnew);//Array ReplicationSize + =numnew; returnNumnew! = 0; }//add multiple elements from a specified location Public BooleanAddAll (intindex, COLLECTION<?extendsE>c) {rangecheckforadd (index); Object[] a=C.toarray (); intNumnew =a.length; Ensurecapacityinternal (Size+ numnew);//Expansion Capacity intnummoved = Size-index; if(nummoved > 0) system.arraycopy (elementdata, index, Elementdata, index+ numnew,nummoved);//Array ReplicationSystem.arraycopy (A, 0, Elementdata, Index, numnew); Size+=numnew; returnNumnew! = 0; }//The addition process is not only related to the number of elements but also to the location to be added
Reomve method
//deletes an element from the specified primary and returns the element to be deleted PublicE Remove (intindex) {Rangecheck (index);//Check Arraymodcount++; E OldValue=Elementdata (index); intnummoved = size-index-1; if(nummoved > 0) system.arraycopy (elementdata, index+1, Elementdata, index,nummoved);//Array moves forwardElementdata[--size] =NULL;//convenient for GC recycling, NULL at the end returnOldValue; }//Delete the same element in the original array as the target object Public BooleanRemove (Object o) {if(O = =NULL) { for(intindex = 0; index < size; index++) if(Elementdata[index] = =NULL) {fastremove (index); return true; } } Else { for(intindex = 0; index < size; index++) if(O.equals (Elementdata[index])) {fastremove (index); return true; } } return false; }
Set/get method
//Assigning a value to a specified position element PublicE Set (intindex, E Element) {Rangecheck (index);//Check Array statusE OldValue = elementdata (index);//check to show array subscript out of bounds exceptionElementdata[index] = element;//Replace the original element value with a new value returnOldValue;//returns the original element value }//gets the value of an element PublicE Get (intindex) {Rangecheck (index);//check to show array subscript out of bounds exception returnElementdata (index);//returns the element value of the index position}
Grow method (for automatic capacity expansion)
Private voidGrowintmincapacity) { intoldcapacity = Elementdata.length;//Raw Capacity intNewcapacity = oldcapacity + (oldcapacity >> 1);//expansion to 1.5 times times the original capacity if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); Elementdata= Arrays.copyof (Elementdata, newcapacity);//array element Replication}
Java ArrayList Source Code Analysis