On the code:
Package Com.itany.myarraylist;import Java.util.iterator;public class Myarraylist<t> implements Iterable<t >{private static final int default_capacity=10;//default array capacity size private int thesize;//number of elements in the collection private t[] Theite ms;//the array in the collection public myarraylist () {thesize=0; Clear (); } public int size () {return thesize; }//Clears all the elements in the collection public void Clear () {thesize=0; Ensurecapacity (default_capacity); } public boolean IsEmpty () {return size () ==0; }//Reduce the size of the array to as large as the number of elements in the collection public void TrimToSize () {ensurecapacity (size ()); } public T get (int idx) {//Note here idx> it is best to write size () because the Get method returns T if write theitems.length may return an empty instead of the corresponding t if (i dx<0 | | Idx>=size ()) throw new ArrayIndexOutOfBoundsException (); return THEITEMS[IDX]; }//Returns the original value modified public T set (int idx,t NewT) {if (idx<0 | | idx>size ()) throw new arrayindex OutofBoundsexception (); T Old=theitems[idx]; THEITEMS[IDX]=NEWT; return old; }//Expand the size of the Theitems array according to the new capacity size and pass the old array contents into the new Theitems public void ensurecapacity (int newcapacity) {//If the new capacity The number of elements in the size < set is the direct end of the method if (newcapacity<thesize) return; T[] Old=theitems; Theitems=new T[newcapacity]; Cannot create a generic array of T cannot direct new generic array theitems= (t[]) new object[newcapacity];//can only write such warnings inevitably//put all old The contents of the array are all given to the new capacity array note that the clear call is not assigned a value for (int i=0;i<size (); i++) {theitems[i]=old[thesize=0 i];//remaining Empty}}//directly by default plus the last public void Add (T newT) {Add (Size (), NewT); } public void Add (int idx,t NewT) {//when inserting, it is necessary to consider whether the number of elements in the collection is equal to the capacity size of the array is responsible for expanding the capacity of the logarithmic group if (size () ==theitems. Length) ensurecapacity (2*size () +1); At this point the array capacity is greater than the number of elements of the collection//for insertion There are two possible 1 if the insertion position >=size () the size of the position so do not need to move 2 another if the insertion position <size () will need to pan//But both need the same statement theITEMS[IDX]=NEWT for (int i=size (); i>idx;i--)//size () >idx If the condition is not satisfied, do not skip. {Theitems[i]=thei TEMS[I-1]; } theitems[idx]=newt;//all situations are thesize++; } public T Remove (int idx) {T oldt=theitems[idx]; for (int i=idx;i<size () -1;i++) {theitems[i]=theitems[i+1]; } thesize--; return Oldt; } @Override//Override the Iterator method to obtain a arraylistiterator iterator that is written by itself public iterator<t> Iterator () {//T ODO auto-generated Method stub return new Arraylistiterator (); }//self-written iterator class Arraylistiterator private class Arraylistiterator implements iterator<t> {private int C urrent=0; @Override public boolean Hasnext () {return current<size (); } @Override public T next () {if (!hasnext ()) throw new Java.util.NoSuchElem Entexception (); Return theitems[current++];//current is called after +1} @Override public void Remove () {//After calling Next, current is +1 so First-1 again delete current//cannot write only this.remove (--current); Because this is the Arraylistiterator object MyArrayList.this.remove (--current); } }}
Package Com.itany.myarraylist;import Java.util.iterator;public class test{public static void Main (string[] args) c1/>{ myarraylist<integer> my=new myarraylist<integer> (); My.add (n); My.add (one); My.add (+); My.add (2,14); My.remove (1); Iterator<integer> it=my.iterator (); while (It.hasnext ()) { System.out.print (It.next () + " "); } } }
Java implementation of data structure--arraylist