Package test;import Java.util.arrays;import java.util.Collection; Public classMyarraylist<e> { //Storing Data Privateobject[] values; //capacity already in use Private intsize; //Number of modifications Private intModcount; /** * @param initialcapacity based on the given capacity*/ PublicmyArrayList (intinitialcapacity) { if(Initialcapacity >0){//Initializes an array with a given capacity sizeValues =NewObject[initialcapacity]; }Else if(Initialcapacity = =0) {Values=Newobject[]{}; }Else{ Throw NewIllegalArgumentException ("illegal initialization of capacity:"+initialcapacity); } } /** * Default initialization method*/ Publicmyarraylist () { This(Ten); } /** * Initialize a arraylist with a set * @param c*/ Publicmyarraylist (Collection c) {values=C.toarray (); if(Values.length! =0) {size=values.length; if(Values.getclass ()! = object[].class) Values= Arrays.copyof (values, size, object[].class); }Else{values=Newobject[]{}; } } /** * Check if the array is out of bounds * @param index*/ Private voidRangecheck (intindex) { if(Index >= Size | | Index <0) Throw NewIndexoutofboundsexception ("the array is out of bounds"); } /** * Replace the value in the original array and return the original value * @param index * @param element * @return*/ PublicESet(intindex,e Element) { //Check to see if it crossesRangecheck (index); E OldValue=(E) Values[index]; Values[index]=element; returnOldValue; } /** * Add an element at the end of the array * @param elements*/ Public voidAdd (E element) {//ExpansionEnsurecapacity (size+1); Values[size++] =element; } /** * @param index * @param element*/ Public voidAddintindex,e Element) { //check that the array subscript is out of bounds firstRangecheck (index); //ExpansionEnsurecapacity (size+1); Object[] OldValues=values; Values[index]=element; //copies the value from the original array to the new arraySystem.arraycopy (oldvalues, index, values, index+1, size-index); Size++; } /** * Add an element from a collection to the end of the array * @param c*/ Public voidAddAll (Collection c) {object[] CArray=C.toarray (); //ExpansionEnsurecapacity (size+carray.length); //Copy the incoming data to the end of the arraySystem.arraycopy (CArray,0, values, size, carray.length); Size+=carray.length; } /** * Adds an element from a collection to the specified position of the array * @param index * @param c*/ Public voidAddAll (intindex,collection c) { //check that the array subscript is out of bounds firstRangecheck (index); //Expansionobject[] CArray =C.toarray (); intClength =carray.length; Ensurecapacity (Size+clength); Object[] OldValues=values; //copies incoming data to an array at the specified locationSystem.arraycopy (CArray,0, values, index, clength); //Copy the data from the index in the original array.System.arraycopy (oldvalues, index, values, values.length-1, size-index); Size+=clength; } /** * @param index * @return to get the element according to the subscript*/ PublicEGet(intindex) { //check that the array subscript is out of bounds firstRangecheck (index); E e=(E) Values[index]; returne; } /** * Delete element according to subscript * @param index * @return*/ PublicE Remove (intindex) { //check that the array subscript is out of bounds firstRangecheck (index); E RemoveValue=(E) Values[index]; System.arraycopy (values, index+1, values, index, size-index-1); //set to NULL, GC recyclevalues[size--] =NULL; returnRemoveValue; } /** * @param by Object Delete **/ Public voidRemove (Object o) {//Consider the case where the element is null if(NULL==o) { for(inti =0; i < size; i++) { if(Values[i] = =NULL) {Remove (i); } } }Else{ for(inti =0; i < size; i++) { if(Values[i].equals (o)) {remove (i); } } } } /** * Expansion method to ensure that the length of the array is sufficient * @param mincapacity*/ Public voidEnsurecapacity (intmincapacity) {Modcount++; intOldcapacity =values.length; if(Mincapacity >oldcapacity) { //The default expansion 50%+1, if still not satisfied, then directly with the incoming required capacity intNewcapacity = (oldcapacity*3)/2+1; if(Newcapacity <mincapacity) {newcapacity=mincapacity; } Values=arrays.copyof (values, newcapacity); } } Public intsize () {returnsize; } }
Finally, the idea of ensurecapacity this method (seen from the Internet):
We know that the interior of ArrayList is to use an array to store elements, because the Java array is fixed-length, so the size of the array must be fixed, this size is capacity. We can be sure that the capacity must be greater than or equal to the size of ArrayList, so when size increases to more than capacity, ArrayList has to recreate the new capacity to accommodate more elements, You need to first create a longer array, copy the elements from the original array to the new array, and then delete the original array. It can be seen that the consumption of this operation is increasing when the ArrayList is getting bigger and larger.
In order to reduce this unnecessary reconstruction capacity operation, when we can be sure ArrayList roughly how big (or at least how big), we can let ArrayList set the capacity to our desired size, in order to avoid redundant array reconstruction.
Assuming that ArrayList automatically sets capacity to 10 and increments the original two-thirds for each rebuild, then when we need to store about 50 elements in ArrayList, we will need to reconstruct the array 4 times, respectively, by adding 11th, 17th, 26th, The 39th element of the time. If we let ArrayList's capacity be 50 in the beginning, then all the insertions can be done without any array rebuilding.
Java allows us to specify capacity, such as new ArrayList (50), while constructing the ArrayList, allowing it to be larger later, while increasing capacity is using the Ensurecapacity () method. Note: The capacity can only be larger than the original, but not smaller than the original, otherwise Java ignores the operation. The initial default capacity of ArrayList is 10, so it makes no sense to specify an integer less than 10 for capacity.
Finally, the size of the ArrayList, said before, size must be less than or equal to capactiy, and more importantly, access to a location larger than size will throw an exception, although this position may not exceed capacity. Ensurecapacity () can only increase capacity without any effect on size. To increase the size, use the Add () method only.
Simulation implementation of Java ArrayList