Add Operation:
Private transientobject[] Elementdata;Private Static Final intDefault_capacity = 10; PublicArrayList () {Super(); This. Elementdata =Empty_elementdata;} PublicArrayList (intinitialcapacity) { Super(); if(Initialcapacity < 0) Throw NewIllegalArgumentException ("Illegal capacity:" +initialcapacity); This. Elementdata =Newobject[initialcapacity];} Public BooleanAdd (e e) {ensurecapacityinternal (size+ 1);//check the array capacity before adding, size+1 indicates the next capacityelementdata[size++] = e;//The initial default value for size is 0, and each added element is self-increment by 1, so size represents the actual array length return true;}Private voidEnsurecapacityinternal (intmincapacity) { if(Elementdata = =empty_elementdata) {mincapacity= Math.max (default_capacity, mincapacity);//if Elementdata is an empty collection, it compares the incoming mincapacity and the default value of 10 to select a larger} ensureexplicitcapacity (mincapacity);}Private voidEnsureexplicitcapacity (intmincapacity) {Modcount++; if(Mincapacity-elementdata.length > 0)//creates a new object if the specified size is elementdata.length to the specified size, otherwise elementdata.length=0; here mincapacity represents the desired array length, Elementdata.length represents the length of the array after expansion, if the array lengthGrow (mincapacity);//Expansion Method}Private voidGrowintmincapacity) { intOldcapacity =elementdata.length; intNewcapacity = oldcapacity + (oldcapacity >> 1);//>>: Right-shift operator, NUM >> 1, equivalent to num divided by 2, where capacity expansion is based on the normal size of the original volume if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); Elementdata= Arrays.copyof (Elementdata, newcapacity);//Copy the original element into a new array after expansion}
Summary: Elementdata is an intermediate array of ArrayList, is the actual data storage array, size is the actual length of the array, Elementdata.length is greater than or equal to size. when declaring a ArrayList array, adding elements to an array after a specified length, if the length exceeds the length of the ArrayList Intermediate Array (elementdata.length), increases the capacity of the array, increasing the capacity of half of the original lengthIf the ArrayList array is declared without a specified length, the default is an empty array, and when adding elements, it is determined that if the current collection (intermediate array) is empty, the default length of 10 is assigned to the array, that is, the array capacity after the first element is added is actually
Delete action:
Remove (int index) deletes the element at the specified position
PublicE Remove (intindex) {Rangecheck (index);//Check if index is out of boundsmodcount++; E OldValue= Elementdata (index);//to remove an element from a position intnummoved = size-index-1; if(nummoved > 0) system.arraycopy (elementdata, index+1, Elementdata, index,nummoved);//copies an array from the specified source array, starting at the specified location and ending at the specified position in the destination array. Elementdata array index+1 position and subsequent elements are copied to Elementdata, receive copy element from index position, receive nummoved elementElementdata[--size] =NULL;//Clear to let GC do it work is about to null recycle the last element returnOldValue;//returns the element that has been removed }
Summary: Deleting an element at a specified position moves the element after that position in the array forward one bit, and the last position null is recycledRemove
(Object o) Deletes the first occurrence of the specified element in this list
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; }//This method is similar to removePrivate voidFastremove (intindex) {Modcount++; intnummoved = size-index-1; if(nummoved > 0) system.arraycopy (elementdata, index+1, Elementdata, Index, nummoved); elementdata[--size] =NULL;//clear to let GC do it work}
Summary: Remove the first to determine whether it is null, if it is to operate alone, because NULL does not have the Equals method, by comparing elements to determine the position of the element to overflow, and then remove the element by a method similar to remove
Modifying/setting (SET) Operation
Set (int Index, E Element) replaces the element at the specified position in this list with the specified element. Public E set (int Index, E Element) { Rangecheck (index); = Elementdata (index); = element; return oldValue; }
Summary: Assigns the element at the specified position directly to the value to be assigned
query/Get (GET) operation:
Public E get (int index) { Rangecheck (index); return Elementdata (index); }
Summary: Returns the array element at the specified position directly
ArrayList Source Code Analysis