The following is an implementation of the ArrayList generic class. To avoid confusion with classes in the class library named myArrayList, the main details are:
- The member variable contains the underlying array, the array capacity, and the current number of items stored in the myarraylist.
- Provides a mechanism to change the capacity of the underlying array. By getting a new array, copy the old array to the new array to change the capacity of the new array, allowing the virtual machine to reclaim the old array.
- Provides implementations of Get () and set ().
- Provides basic operations such as size (), IsEmpty (), and Clear (), as well as remove (IDX), and add (x) and add (IDX,X) operations. If the array size and capacity are the same, then these two add operations will expand capacity.
- Provides a class that implements the iterator interface. This class stores the subscript for the next item in the iteration sequence and provides the implementation of methods such as Next (), Hasnext (), and remove (). The iterator method returns a new instance of the class that implements the iterator interface directly.
Import Java.util.iterator;import Java.util.nosuchelementexception;public class myarraylist<anytype> Implements iterable<anytype> {private static final int default_capacity = 10;private int thesize;private anytype[] t Heitems;public myarraylist () {clear ();} public void Clear () {thesize = 0;ensurecapacity (default_capacity);} public int size () {return thesize;} public Boolean isEmpty () {return size () = = 0;} public void Trumtosize () {ensurecapacity (size ());} Public AnyType get (int idx) {if (idx < 0 | | idx >= size ()) {throw new ArrayIndexOutOfBoundsException ();} return THEITEMS[IDX];} Public AnyType Set (int idx, AnyType newval) {if (idx < 0 | | idx >= size ()) {throw new arrayindexoutofboundsexception ();} AnyType old = Theitems[idx];theitems[idx] = Newval;return old;} @SuppressWarnings ("unchecked") public void ensurecapacity (int newcapacity) {if (Newcapacity < size ()) {return;} anytype[] Old = Theitems;theitems = (anytype[]) new object[newcapacity];for (int i = 0; i < size (); i++) {Theitems[i] = Old[i];}} public void Add (AnyType x) {Add (Size (), x);} public void Add (int idx, AnyType x) {if (Theitems.length = = Size ()) {ensurecapacity (Size () * 2 + 1);} for (int i = size (); i > idx; i--) {theitems[i] = theitems[i-1];} THEITEMS[IDX] = x;thesize++;} Public AnyType Remove (int idx) {AnyType Removeditem = theitems[idx];for (int i = idx; i < size ()-1; i++) {theitems[i] = Theitems[i + 1];} Thesize--;return Removeditem;} Public iterator<anytype> Iterator () {return new Arraylistiterator ();} Private class Arraylistiterator implements iterator<anytype> {private int current = 0;public Boolean Hasnext () {Retu RN Current < Size ();} Public AnyType Next () {if (!hasnext ()) {throw new Nosuchelementexception ();} return theitems[current++];} public void Remove () {MyArrayList.this.remove (--current);}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure (Java language)--arraylist