Package mylist; import Java. util. arrays; import Java. util. concurrentmodificationexception; import Java. util. iterator;/*** a simple collection class that simulates arraylist ** @ author Jiang Xia ** @ Param <E> */public class myarraylist <E> implements mylist <E>, java. io. serializable {Private Static final long serialversionuid = 298503308405784327l;/** when designing a dynamic array, it is essentially an array. * therefore, this class should have at least two attributes, * 1. array elementdata * 2. actual size of the array *//*** Array transient modifier: the object to be modified will not be retained during stream transmission * (the class of the object to be modified must be implemented in Java. io. serializable Interface) * elementdata is a cache array, which usually reserves some capacity and expands the capacity when the capacity is insufficient. * The owner must understand it and does not need to explain it more. If there are actually five elements, and the elementdata size may be 10, * only five elements need to be stored during serialization, the last five elements in the array are meaningless and do not need to be stored. * Elementdata is designed as transient, and is manually serialized in the writeobject method. * only those elements actually stored are serialized, instead of the entire array * (actually refactoring an array to make it size = Data Count) */@ suppresswarnings ("UNUSED") Private transient object [] elementdata; /*** actual array size (size = Data Count) */@ suppresswarnings ("UNUSED") Private int size; /*** record the number of times a dynamic array is restructured */private transient int modcount = 0;/** the first experience of a dynamic array is when it is created, * We can directly create the desired array without specifying the array size. * This is due to the constructor in the dynamic array. * Our constructor will set 2x1. no parameter construction, but the declaration of the array must require an initial size, so we will give the array an initial value in the no parameter construction * 2. if we have parameter construction (array size), sometimes we know the number of data, * We Want to manually set the array size to reduce the overhead of dynamic arrays when creating arrays * 3. sometimes we want to convert an array into a dynamic array * // ** without parameters, but the array declaration requires an initial size, * therefore, we will give the array an initial value */Public myarraylist () {This (10);}/*** to construct a parameter (array size ), sometimes we know the number of data, * so we want to manually set the size of the array, to reduce the overhead of dynamic array creation * @ Param elementsize * set the array size */Public myarraylist (INT elementsize) {If (elementsize <0) {thro W new illegalargumentexception ("Your array size settings are incorrect:" + elementsize);} else {This. elementdata = new object [elementsize] ;}/ *** set the dynamic array capacity to the actual array size (capacity = actual size) */Public void trimtosize () {This. modcount ++; int oldelementsize = This. elementdata. length; If (this. size <oldelementsize) {// arrays. copyof () and system. arraycopy () works the same way, except that arraycopy () needs to specify the starting position of the original array, the starting position of the destination array and the destination array. The copy length is obviously rigid, therefore, copyof () is used to directly transmit the destination array and the copy length. This. elementdata = arrays. copyof (this. elementdata, size); // jdk1.6 new addition method}/*** determines the dynamic array capacity if the current array capacity is insufficient to load data, then scale out * @ Param mincapacity * Actual size + 1 (indicating the added data coordinates) */Public void ensurecapacity (INT mincapacity) {This. modcount ++; // get the current array length int oldelementszie = This. elementdata. length; // compare the minimum capacity with the current array length // if the minimum capacity is greater than the current array length, reverse thinking based on golden splitting, create a new array length to reconstruct the array if (mincapacity> oldelementszie) {int newelementsize = (oldelementsz IE * 3)/2 + 1; // if the minimum capacity is greater than the new array length, the new array length is equal to the minimum capacity, because the minimum capacity is usually close to the size, so this is a win-win if (mincapacity> newelementsize) {newelementsize = mincapacity;} This. elementdata = arrays. copyof (this. elementdata, newelementsize) ;}@overridepublic int size () {return this. size ;}@ overridepublic Boolean isempty () {return this. size = 0 ;}@ overridepublic Boolean contains (Object OBJ) {return indexof (OBJ)> = 0 ;}@ overridepublic iterat Or <E> iterator () {return NULL;} @ overridepublic object [] toarray () {return Java. util. arrays. copyof (elementdata, size) ;}@ overridepublic Boolean add (E) {// determine whether to expand this. ensurecapacity (this. size + 1); // Add data this. elementdata [size ++] = E; return true ;}@ overridepublic Boolean remove (Object O) {// if the object is null, the array is traversed, search for the first empty object and delete if (O = NULL) {for (INT Index = 0; index <size; index ++) if (elementda Ta [Index] = NULL) {// deletion method fastremove (INDEX); return true ;}} else {// if the object is not empty, the array is traversed, find the first element that matches the object, and delete for (INT Index = 0; index <size; index ++) if (O. equals (elementdata [Index]) {// deletion method fastremove (INDEX); return true ;}return false ;}@ overridepublic Boolean containsall (mylist <E> List) {int temp = 0; For (INT I = 0; I <list. size (); I ++) {If (this. contains (list. get (I) {temp ++ ;}} if (temp = List. size () {Return true;} return false;} @ overridepublic Boolean removeall (mylist <E> List) {for (INT I = 0; I <list. size (); I ++) {If (this. contains (list. get (I) {This. remove (list. get (I) ;}} return true ;}@ overridepublic Boolean addall (mylist <E> List) {int numnew = List. size (); // whether to expand ensurecapacity (size + numnew); // adjust the value of the increments modcount element to the right of the system. arraycopy (list. toarray (), 0, elementdata, size, numnew );// Update the actual size of the array. Size + = numnew; return numnew! = 0 ;}@ overridepublic Boolean addall (INT index, mylist <E> List) {// determines whether the index meets requirements if (index> size | index <0) throw new indexoutofboundsexception ("index:" + index + ", size:" + size); int numnew = List. size (); // whether to expand ensurecapacity (size + numnew); // increments modcountint nummoved = size-index; If (nummoved> 0) system. arraycopy (elementdata, index, elementdata, index + numnew, nummoved); system. arraycop Y (list. toarray (), 0, elementdata, index, numnew); size + = numnew; return numnew! = 0 ;}@ overridepublic Boolean retainall (mylist <E> List) {// determines whether the parameter array is empty. If it is empty, elementdataif (list. size () = 0) {This. clear ();} object [] newobjs = new object [list. size ()]; int temp = 0; // determines whether the intersection is null // traverses the parameter array for (INT I = 0; I <list. size (); I ++) {// compare with each element in the array // if the same element exists, a new elementdata is created, put consistent data into the new array if (this. contains (list. get (I) {newobjs [I] = List. get (I); temp ++; }}if (temp = 0) {This. clear ();} el Se {This. elementdata = arrays. copyof (newobjs, newobjs. length); this. size = newobjs. length;} return true ;}@ overridepublic void clear () {// Number of refactored arrays + 1 modcount ++; // traverses the array, set all elements to NULL for (INT I = 0; I <size; I ++) elementdata [I] = NULL; // the actual size of the array is 0 size = 0 ;}@ overridepublic e get (INT index) {rangecheck (INDEX); Return (e) elementdata [Index];} @ overridepublic e set (INT index, e element) {rangecheck (INDEX); object ol Delement = This. elementdata [Index]; this. elementdata [Index] = element; Return (e) oldelement ;}@ overridepublic void add (INT index, e element) {// determine whether the index meets the requirements if (index> size | index <0) throw new indexoutofboundsexception ("index:" + index + ", size:" + size ); // determine whether to expand ensurecapacity (size + 1); // increments modcount !! // Shifts the element right according to the specified index (copying the array) system. arraycopy (elementdata, index, elementdata, index + 1, size-index); // Add the new element elementdata [Index] = element; // follow the actual size of the new array. Size ++ ;} @ overridepublic e remove (INT index) {// determines whether the index is out of bounds rangecheck (INDEX); // increase the number of changes modcount ++; // obtain the specified Element E oldvalue = (e) elementdata [Index]; // obtain the last index int nummoved = size-index-1; // If the index is greater than 0, an element exists and the element is moved left (in fact, array replication is implemented) if (nummoved> 0) system. arraycop Y (elementdata, index + 1, elementdata, index, nummoved); elementdata [-- size] = NULL; // Let GC do its workreturn oldvalue ;} @ overridepublic int indexof (Object O) {// checks whether the object is empty. // if it is null, the array is traversed cyclically and the element to be compared is empty, returns the index indexif (O = NULL) {for (INT I = 0; I <this. size; I ++) {If (this. elementdata [I] = NULL) {return I ;}} else {// cyclically traverses the array if it is not null and compares each element, returns the index indexfor (INT I = 0; I <this. size; I ++) {if (This. elementdata [I]. equals (O) {return I ;}}// otherwise-1 return-1 ;}@ overridepublic int lastindexof (Object O) {// determines whether the object is null. // if it is null, the array is traversed cyclically. If it is null, the index indexif (O = NULL) of the First Matching Element is returned) {for (INT I = This. size-1; I> = 0; I --) {If (this. elementdata [I] = NULL) {return I ;}} else {// cyclically traverses the array if it is not null and compares each element, returns the index indexfor (INT I = This. size-1; I> = 0; I --) {If (this. elementdata [I]. equals (O) {return I ;}}// otherwise-1 return-1 ;}@ overridepublic object [] sublist (INT fromindex, int toindex) {If (fromindex> toindex) {Throw new illegalargumentexception ("your start subscript and end small mark are incorrect: fromindex =" + fromindex + "toindex =" + toindex );} object [] newobjs = new object [toindex-fromindex]; Int J = 0; For (INT I = fromindex; I <= toindex; I ++) {newobjs [J] = This. elementdata [I]; j ++;} return newobjs;}/*** determines whether the parameter index is larger than the actual array size ** @ Param Index */private void rangecheck (INT index) {If (index> = This. size) {Throw new illegalargumentexception ("your coordinates have exceeded the array size: size =" + this. size + "Index =" + index) ;}} private void fastremove (INT index) {modcount ++; int nummoved = size-index-1; if (nummoved> 0) system. arraycopy (elementdata, index + 1, elementdata, index, nummoved); elementdata [-- size] = NULL;} Public String tostring () {This. tr Imtosize (); stringbuffer STR = new stringbuffer (); Str. append ("["); For (INT I = 0; I <this. elementdata. length; I ++) {if (I = This. elementdata. length-1) {Str. append (this. elementdata [I]);} else {Str. append (this. elementdata [I] + ",") ;}} Str. append ("]"); Return Str. tostring ();} private void writeobject (Java. io. objectoutputstream s) throws Java. io. ioexception {// write out element count, and any hidden s Tuffint expectedmodcount = modcount; S. defaultwriteobject (); // write out array lengths. writeint (elementdata. length); // write out all elements in the proper order. for (INT I = 0; I <size; I ++) s. writeobject (elementdata [I]); If (modcount! = Expectedmodcount) {Throw new concurrentmodificationexception () ;}} private void readobject (Java. io. objectinputstream s) throws Java. io. ioexception, classnotfoundexception {// read in size, and any hidden stuffs. defaultreadobject (); // read in array length and allocate arrayint arraylength = S. readint (); object [] A = elementdata = new object [arraylength]; // read in all elements in the proper order. for (INT I = 0; I <size; I ++) A [I] = S. readobject () ;}public int getmodcount () {return modcount ;}}