The default initial capacity is ten, and the underlying object is implemented using an array of objects.
public void ensurecapacity (int mincapacity). Ensure that the minimum capacity of the array is used when adding elements.
Its parent class abstractlist has only one abstract method, abstract public E get(int index);
modcount: Record the number of times the list structure is changed, in fact, change the size will be self-increment, mainly add and Remove,set does not make modcount self-increment.
Affects the operation of Modcount: Add,remove, Ensurecapacityinternal, Fastremove, clear, TrimToSize, RemoveRange (those that change capacity ).
There is also a listiterator() method that returns an iterator.
The code for expansion:
Public voidEnsurecapacity (intmincapacity) { if(Mincapacity > 0) ensurecapacityinternal (mincapacity);}Private voidEnsurecapacityinternal (intmincapacity) { Modcount ++; //overflow-conscious Code if(Mincapacity-elementdata.length > 0) grow (mincapacity);}Private voidGrowintmincapacity) { //overflow-conscious Code intOldcapacity =elementdata.length;
1.5 times times the original capacityint newcapacity = oldcapacity + (oldcapacity >> 1); if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); //mincapacity is usually close to size, so this is a win:Elementdata =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;}
Look at one more way:
Public void trimtosize () { Modcount++ ; int oldcapacity = elementdata.length; if (Size < oldcapacity) { = arrays.copyof (elementdata, size); } }
The length of the Elementdata is expanded, and thesize tag is the number of elements contained therein. So there will be a small size but elementdata.length a large situation , there will be wasted space . TrimToSize will return a new array to Elementdata, the element content remains unchanged,length is the same as size, saving space
private void Fastremove (int index): does not perform bounds checking and does not return deleted values.
Clear method:AndNot modifiedElementdataOflength, just put all the elementsReset toNull, the size is set to 0.
Sublistthe removerange method that invokes the parent class (ArrayList)。
Java Collection (3)--arraylist Source analysis