If a value type is inserted in the arraylist, the packing operation is triggered, and the value type needs to be removed, as shown below:
Arraylist myarraylist = new arraylist (); myarraylist. add (40); // bind myarraylist. add (80); // boxed int32 a1 = (int32) myarraylist [0]; // unpack int32 a2 = (int32) myarraylist [1]; // unpack
This results in performance consumption. For more information about packing, see the next article.
To solve these problems, C # has an ilist <t> interface that supports generics. The detailed code below shows that the structure is the same as that of ilist, but the generics are added.
/// <Summary> /// generic collection class /// </Summary> /// <typeparam name = "T"> </typeparam> public class list <t>: ilist <t>, ilist {// <summary> /// generic iterator /// </Summary> /// <typeparam name = "T"> </typeparam> public struct enumertor <t>: ienumerator, ienumerator <t> {// iterative index private int index; // The collection object to which the iterator belongs references private list <t> list; Public enumertor (list <t> container) {This. list = container; this. index =-1;} public Void dispose () {}/// <summary> // display the current attribute of ienumerator /// </Summary> Object ienumerator. current {get {return list [Index];} /// <summary> /// implement the current attribute of ienumerator <t> /// </Summary> Public t current {get {return list [Index];} /// <summary> /// iterator indicates the next Data Location /// </Summary> /// <returns> </returns> Public bool movenext () {If (this. index <list. count) {++ this. index;} return thi S. index <list. count;} public void reset () {This. index =-1 ;}//< summary> // an array for storing data. The T type represents the function of generics. /// </Summary> private T [] array; // <summary> // The length of the current set /// </Summary> private int count; /// <summary> /// default constructor /// </Summary> public list (): This (1) {} public list (INT capacity) {If (capacity <0) {Throw new exception ("the initial length of the set cannot be less than 0");} If (capacity = 0) {capacity = 1;} This. array = new T [capacity];} // <summary> // set length // </Summary> Public int count {get {return this. count ;}//< summary> /// actual length of the Set /// </Summary> Public int capacity {get {return this. array. length ;}/// <summary> /// whether the size is fixed /// </Summary> Public bool isfixedsize {get {return false ;}} /// <summary> /// read-only/// </Summary> Public bool isreadonly {get {return false ;}} /// <summary> /// whether the attribute can be the same // </Summary> Public bool issynchronized {get {return false ;}} /// <summary> /// synchronization object /// </Summary> Public object syncroot {get {return NULL ;}} /// <summary> /// when the length is not enough, reassign an array with sufficient length /// </Summary> /// <returns> </returns> private T [] getnewarray () {return New T [(this. array. length + 1) * 2];} /// <summary> /// implement the ilist <t> Add method // </Summary> /// <Param name = "value"> </param> Public void add (T value) {int newcount = This. count + 1; if (this. array. length <newcount) {T [] newarray = getnewarray (); array. copy (this. array, newarray, this. count); this. array = newarray;} This. array [this. count] = value; this. count = newcount ;} /// <summary> /// add an object to the end of the Set /// </Summary> /// <Param name = "value"> </param> // <returns> </returns> int ilist. add (object Value) {(ilist <t>) This ). add (t) value); return this. count-1 ;} /// <summary> /// implement ilist <t> indexer // </Summary> /// <Param name = "Index"> </param> // /<returns> </returns> Public t this [int Index] {get {If (index <0 | index> = This. count) {Throw new argumentoutofrangeexception ("Index");} return this. array [Index];} set {If (index <0 | index> = This. count) {Throw new argumentoutofrangeexception ("Index");} This. array [Index] = value ;}} /// <summary> /// display the indexer that implements the ilist interface /// </Summary> /// <Param name = "Index"> </param> // /<returns> </returns> Object ilist. this [int Index] {get {return (ilist <t>) This) [Index];} set {(ilist <t>) This) [Index] = (t) value ;}} /// <summary> /// Delete the element in the Set /// </Summary> /// <Param name = "Index"> </param> /// <Param name = "count"> </param> Public void removerange (INT index, int count) {If (index <0) {Throw new argumentoutofrangeexception ("Index");} int removeindex = index + count; If (count <0 | removeindex> This. count) {Throw new argumentoutofrangeexception ("Index");} array. copy (this. array, index + 1, this. array, index + count-1, this. count-removeindex); this. count-= count ;} /// <summary> /// implement the indexof method of the ilist <t> interface /// </Summary> /// <Param name = "value"> </param> /// <returns> </returns> Public int indexof (T value) {int Index = 0; If (value = NULL) {While (index <this. count) {If (this. array [Index] = NULL) {Return Index ;}++ index ;}} else {While (index <this. count) {If (value. equals (this. array [Index]) {Return Index ;}++ index ;}} return-1 ;} /// <summary> /// display the indexof method implementing the ilist interface // </Summary> /// <Param name = "value"> </param> // /<returns> </returns> int ilist. indexof (object Value) {return (ilist <t>) This ). indexof (t) value );} /// <summary> /// find the corresponding array item /// </Summary> /// <Param name = "O"> </param> /// <Param name = "COMPAR"> </param> // <returns> </returns> Public int indexof (Object O, icomparer COMPAR) {int Index = 0; while (index <this. count) {If (COMPAR. compare (this. array [Index], O) = 0) {Return Index ;}++ index ;}return-1 ;} /// <summary> /// Remove Method for implementing the ilist <t> interface /// </Summary> /// <Param name = "value"> </param> /// <returns> </returns> Public bool remove (T value) {int Index = This. indexof (value); If (index> = 0) {This. removerange (index, 1); Return true;} return false;} // <summary> // display the remove method that implements the ilist interface, the implementation /// </Summary> /// <Param name = "value"> </param> void ilist. remove (object Value) {(ilist <t>) This ). remove (t) value );} /// <summary> /// Delete the object reference from the specified position in the Set /// </Summary> /// <Param name = "Index"> </param> Public void removeat (INT index) {removerange (index, 1 );} /// <summary> /// the last element of the set is displayed /// </Summary> /// <returns> </returns> Public object popback () {object o = This. array [this. count-1]; removeat (this. count-1); return O ;} /// <summary> /// the first object in the set is displayed. /// </Summary> /// <returns> </returns> Public object popfront () {object o = This. array [0]; removeat (0); return O ;} /// <summary> /// insert method for implementing the ilist <t> interface /// </Summary> /// <Param name = "Index"> </param> /// <Param name = "value"> </param> Public void insert (INT index, T value) {If (index> = This. count) {Throw new argumentoutofrangeexception ("Index");} int newcount = This. count + 1; if (this. array. length <newcount) {T [] newarray = getnewarray (); array. copy (this. array, newarray, index); newarray [Index] = value; array. copy (this. array, index, newarray, index + 1, this. count-index); this. array = newarray;} else {array. copy (this. array, index, this. array, index + 1, this. count-index); this. array [Index] = value;} This. count = newcount ;} /// <summary> /// display the insert method that implements the ilist interface /// </Summary> /// <Param name = "Index"> </param> // /<Param name = "value"> </param> void ilist. insert (INT index, object Value) {(ilist <t>) This ). insert (index, (t) value );} /// <summary> /// contains method for implementing the ilist <t> interface /// </Summary> /// <Param name = "value"> </param> /// <returns> </returns> Public bool contains (T value) {return this. indexof (value)> = 0 ;} /// <summary> /// display the contains method that implements the ilist <t> interface /// </Summary> /// <Param name = "value"> </Param >/// <returns> </returns> bool ilist. contains (object Value) {return (ilist <t>) This ). indexof (t) value)> = 0 ;}/// <summary> // compress the set to the actual length. /// </Summary> Public void trimtosize () {If (this. array. length> This. count) {T [] newarray = NULL; If (this. count> 0) {newarray = new T [this. count]; array. copy (this. array, newarray, this. count);} else {newarray = new T [1];} This. array = newarray; }}/// <summary> // clear the set /// </Summary> Public void clear () {This. count = 0 ;} /// <summary> /// getenumerator method for implementing the ienumerable interface /// </Summary> /// <returns> </returns> Public ienumerator <t> getenumerator () {enumertor <t> ator = new enumertor <t> (this); Return ator ;} /// <summary> /// display the getenumerator method that implements the ienumerable interface /// </Summary> /// <returns> </returns> ienumerator ienumerable. getenumerator () {return (ienumerable <t>) This ). getenumerator ();} /// <summary> /// copyto method for implementing the icollection <t> interface /// </Summary> /// <Param name = "array"> </param> /// <Param name = "Index"> </param> Public void copyto (T [] array, int index) {array. copy (this. array, 0, array, index, this. count );} /// <summary> /// display implementation icollection <t> copyto method of the interface /// </Summary> /// <Param name = "array"> </ param> // <Param name = "Index"> </param> void icollection. copyto (array, int index) {array. copy (this. array, 0, array, index, this. count );}}
Call:
Static void main (string [] ARGs) {// because int has been specified, The binning operation is not performed when the value type is added. List <int> tlist = new list <int> (); tlist. add (25); tlist. add (30); foreach (int n in tlist) {console. writeline (n);} console. readline ();}
Code: http://download.csdn.net/detail/yysyangyangyangshan/4787961