您好,欢迎访问一九零五行业门户网

C#基础知识整理 基础知识(17)ILiest接口——泛型

对于arraylist中如果插入值类型会引发装箱操作,而取出值类型又需要拆箱,如下
arraylist myarraylist = new arraylist(); myarraylist.add(40);//装箱 myarraylist.add(80);//装箱 int32 a1 = (int32)myarraylist[0];//拆箱 int32 a2 = (int32)myarraylist[1];//拆箱
从而造成性能的消耗。至于装箱的详细解说见下一篇。
为了解决这些问题,c#中有支持泛型的ilist<t>接口,下面看详细代码,其实结构都和ilist一样,只是增加了泛型。
/// <summary> /// 泛型集合类 /// </summary> /// <typeparam name="t"></typeparam> public class list<t> : ilist<t>, ilist { /// <summary> /// 泛型迭代器 /// </summary> /// <typeparam name="t"></typeparam> public struct enumertor<t> : ienumerator, ienumerator<t> { //迭代索引 private int index; //迭代器所属的集合对象引用 private list<t> list; public enumertor(list<t> container) { this.list = container; this.index = -1; } public void dispose() { } /// <summary> /// 显示实现ienumerator的current属性 /// </summary> object ienumerator.current { get { return list[index]; } } /// <summary> /// 实现ienumerator<t>的current属性 /// </summary> public t current { get { return list[index]; } } /// <summary> /// 迭代器指示到下一个数据位置 /// </summary> /// <returns></returns> public bool movenext() { if (this.index < list.count) { ++this.index; } return this.index < list.count; } public void reset() { this.index = -1; } } /// <summary> /// 保存数据的数组,t类型则体现了泛型的作用。 /// </summary> private t[] array; /// <summary> /// 当前集合的长度 /// </summary> private int count; /// <summary> /// 默认构造函数 /// </summary> public list() : this(1) { } public list(int capacity) { if (capacity < 0) { throw new exception("集合初始长度不能小于0"); } if (capacity == 0) { capacity = 1; } this.array = new t[capacity]; } /// <summary> /// 集合长度 /// </summary> public int count { get { return this.count; } } /// <summary> /// 集合实际长度 /// </summary> public int capacity { get { return this.array.length; } } /// <summary> /// 是否固定大小 /// </summary> public bool isfixedsize { get { return false; } } /// <summary> /// 是否只读 /// </summary> public bool isreadonly { get { return false; } } /// <summary> /// 是否可同属性 /// </summary> public bool issynchronized { get { return false; } } /// <summary> /// 同步对象 /// </summary> public object syncroot { get { return null; } } /// <summary> /// 长度不够时,重新分配长度足够的数组 /// </summary> /// <returns></returns> private t[] getnewarray() { return new t[(this.array.length + 1) * 2]; } /// <summary> /// 实现ilist<t>add方法 /// </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> /// 向集合末尾添加对象 /// </summary> /// <param name="value"></param> /// <returns></returns> int ilist.add(object value) { ((ilist<t>)this).add((t)value); return this.count - 1; } /// <summary> /// 实现ilist<t>索引器 /// </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> /// 显示实现ilist接口的索引器 /// </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> /// 删除集合中的元素 /// </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> /// 实现ilist<t>接口的indexof方法 /// </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> /// 显示实现ilist接口的indexof方法 /// </summary> /// <param name="value"></param> /// <returns></returns> int ilist.indexof(object value) { return ((ilist<t>)this).indexof((t)value); } /// <summary> /// 查找对应数组项 /// </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> /// 实现ilist<t>接口的remove方法 /// </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> /// 显示实现ilist接口的remove方法,此处显示实现 /// </summary> /// <param name="value"></param> void ilist.remove(object value) { ((ilist<t>)this).remove((t)value); } /// <summary> /// 从集合指定位置删除对象的引用 /// </summary> /// <param name="index"></param> public void removeat(int index) { removerange(index, 1); } /// <summary> /// 弹出集合的最后一个元素 /// </summary> /// <returns></returns> public object popback() { object o = this.array[this.count - 1]; removeat(this.count - 1); return o; } /// <summary> /// 弹出集合第一个对象 /// </summary> /// <returns></returns> public object popfront() { object o = this.array[0]; removeat(0); return o; } /// <summary> /// 实现ilist<t>接口的insert方法 /// </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> /// 显示实现ilist接口的insert方法 /// </summary> /// <param name="index"></param> /// <param name="value"></param> void ilist.insert(int index, object value) { ((ilist<t>)this).insert(index, (t)value); } /// <summary> /// 实现ilist<t>接口的contains方法 /// </summary> /// <param name="value"></param> /// <returns></returns> public bool contains(t value) { return this.indexof(value) >= 0; } /// <summary> /// 显示实现ilist<t>接口的contains方法 /// </summary> /// <param name="value"></param> /// <returns></returns> bool ilist.contains(object value) { return ((ilist<t>)this).indexof((t)value) >= 0; } /// <summary> /// 将集合压缩为实际长度 /// </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> /// 清空集合 /// </summary> public void clear() { this.count = 0; } /// <summary> /// 实现ienumerable接口的getenumerator方法 /// </summary> /// <returns></returns> public ienumerator<t> getenumerator() { enumertor<t> ator = new enumertor<t>(this); return ator; } /// <summary> /// 显示实现ienumerable接口的getenumerator方法 /// </summary> /// <returns></returns> ienumerator ienumerable.getenumerator() { return ((ienumerable<t>)this).getenumerator(); } /// <summary> /// 实现icollection<t>接口的copyto方法 /// </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> /// 显示实现实现icollection<t>接口的copyto方法 /// </summary> /// <param name="array"></param> /// <param name="index"></param> void icollection.copyto(array array, int index) { array.copy(this.array, 0, array, index, this.count); } }
调用:
static void main(string[] args) { //由于已经指定了int,因此加入值类型不会有装箱拆箱操作。 list<int> tlist = new list<int>(); tlist.add(25); tlist.add(30); foreach (int n in tlist) { console.writeline(n); } console.readline(); }
以上就是c#基础知识整理 基础知识(17)iliest接口——泛型 的内容。
其它类似信息

推荐信息