经由过程MSDN能够看到IList接口有两种:
元素为object范例的IList接口,能够放差别范例的对象援用;
IList<T>泛型接口,只能寄存指定范例的对象援用。
实在,IList和IList<T>也称之为向量,特性是能够动态的转变鸠合的长度,无需肯定鸠合的初始长度,鸠合会跟着寄存数据的数目自动变化。
能够看到IList和IList<T>的继续关联:
[ComVisibleAttribute(true)] public interface IList : ICollection, IEnumerable public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
如今再返回去看下,IList和IList<T>的区分,看以下代码,ArrayList是继续IList的,List继续IList<T>:
public class IListClass { void test() { TestClass1 c1 = null; ArrayList arryList = new ArrayList(); arryList.Add(c1); List<TestClass1> list = new List<TestClass1>(); list.Add(c1); //取值 TestClass1 getC1Array = arryList[0] as TestClass1;//必需要一次强迫转换 TestClass1 getC1List = list[0];//不需要转换,所谓泛型 } } public class TestClass1 { }
这下就比较邃晓了。
一、IList接口概述
ILis接口从ICollection接口继续,具有以下特征,
Count属性——猎取鸠合元素个数;
GetEnumerator要领——能够迭代;
CopyTo要领——将指定元素复制到另一个数组中;
Clear要领——清空悉数鸠合。
IList新增特征,
索引器属性——依据索引接见鸠合中恣意元素;
Add要领——向鸠合末端增添元素;
Insert要领——向鸠合指定位置插进去元素;
Remove要领——移除指定元素;(包括RemoveAt)
Contains要领——推断对象是不是在鸠合中;
IndexOf要领——查找指定对象在鸠合中的索引位置。
别的,IList接口鸠合根据递次寄存元素,不转变元素寄存递次。
2、算法
向量鸠合和数组一样,具有随即接见的特性。即不管接见向量鸠合的任何一个单位,所需的接见时候是完全雷同的。在向量类中,现实上依旧运用一般数组来纪录鸠合数据,向量类运用了一些算法技能,让悉数类对外表现差别于一般数组的主要特性:能够动态转变数组长度。详细算法以下:
在内部数组充足长的情况下,直接举行增添和插进去操纵,在内部数组长度不足的情况下,根据内部数组长度增添2倍作为新的数组的长度,然后举行数据搬移(即把就数组数组移到新数组中)。向量在分派元素存储空间时,会多分派一些冗余空间,只管削减内存分派次数。在数据删除时,并不转变内部数组长度,仅仅是运用被删除数据以后的数据掩盖被删除的数据。
不过向量每次分派空间时都多分派一些冗余空间,会形成内存的压力,因此在顺序中应当只管防止集合的次数繁多的内存分派。
三、完成类
IList和IList<T>的完成类,分别是ArrayList类和List<T>类。
ArrayList类处于System.Collection定名空间下;
List<T>类处于System.Collection.Specialized定名空间下。
四、完成代码(非泛型)
/// <summary> /// 完成IList,非泛型 /// </summary> public class ArrayList : IList { /// <summary> /// 迭代 /// </summary> public struct Enumertor : IEnumerator { /// <summary> /// 迭代索引 /// </summary> private int index; /// <summary> /// 迭代器所属的向量类对象的援用 /// </summary> private ArrayList arrayList; /// <summary> /// 组织函数 /// </summary> /// <param name="arrayList">迭代器所属的鸠合类</param> public Enumertor(ArrayList arrayList) { this.arrayList = arrayList; this.index = -1; } /// <summary> /// 猎取当前对象,依据index的值返回向量对应的对象援用 /// </summary> public object Current { get { return arrayList[index]; } } /// <summary> /// 将迭代器指向下一个数据位置,经由过程转变index的值,加1或减1 /// </summary> /// <returns></returns> public bool MoveNext() { if (this.index < arrayList.Count) { ++this.index; } return this.index < arrayList.Count; } /// <summary> /// 迭代器回到肇端位置,将index置为-1 /// </summary> public void Reset() { this.index = -1; } } /// <summary> /// 保留鸠合的数组 /// </summary> private object[] array = new object[1]; /// <summary> /// 当前鸠合的长度 /// </summary> private int count; /// <summary> /// 默许组织函数 /// </summary> public ArrayList() { } /// <summary> /// 参数组织函数,经由过程参数指定内部数组长度,削减从新分派空间 /// </summary> /// <param name="capacity"></param> public ArrayList(int capacity) { if (capacity < 0) { throw new Exception(); } if (capacity == 0) { capacity = 1; } this.array = new object[capacity]; this.count = 0; } 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> /// 当array长度不足时,从新分派新的长度充足的数组 /// </summary> /// <returns></returns> private object[] GetNewArray() { return new object[(this.array.Length + 1) * 2]; } public int Add(object value) { int newCount = this.count + 1; if (this.array.Length < newCount)//长度不足 { object[] newArray = GetNewArray(); Array.Copy(this.array, newArray, this.count); this.array = newArray;//从新援用,指向新数组 } //增添新元素 this.array[this.count] = value; this.count = newCount; //返回新元素的索引位置 return this.count - 1; } /// <summary> /// 索引器属性,按索引返回向量中的某一项 /// </summary> /// <param name="index"></param> /// <returns></returns> public object this[int index] { get { if (index < 0 || index >= this.count) { throw new Exception(); } return this.array[index]; } set { if (index < 0 || index >= this.count) { throw new Exception(); } this.array[index] = value; } } /// <summary> /// 删除鸠合中的元素 /// </summary> /// <param name="index"></param> /// <param name="count"></param> public void RemoveRange(int index, int count) { if (index < 0) { throw new Exception(); } int removeIndex = index + count;//盘算鸠合中末了一个被删元素的索引 if (count < 0 || removeIndex > this.count) { throw new Exception(); } //删除现实上是将要删除元素以后的一切元素拷贝到要删除元素的位置掩盖掉 Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex); //从新设置鸠合长度 this.count -= count; } /// <summary> /// 查找对应的数组项,现实是遍历查找 /// </summary> /// <param name="value"></param> /// <returns></returns> public int IndexOf(object 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 (this.array[index].Equals(value)) { return index; } ++index; } } return -1; } /// <summary> /// 从鸠合中删除指定元素 /// </summary> /// <param name="value"></param> public void Remove(object value) { int index = this.IndexOf(value); if (index >= 0) { this.RemoveRange(index, 1); } } /// <summary> /// 从鸠合中删除指定位置的元素 /// </summary> /// <param name="index"></param> public void RemoveAt(int index) { RemoveRange(index, 1); } /// <summary> /// 猎取末了一个元素的援用后删除末了一个元素 /// </summary> /// <returns></returns> public object PopBack() { object obj = this.array[this.count - 1]; RemoveAt(this.count - 1); return obj; } /// <summary> /// 猎取第一个元素援用并删除第一个元素 /// </summary> /// <returns></returns> public object PropFront() { object obj = this.array[0]; RemoveAt(0); return obj; } /// <summary> /// 插进去元素 /// </summary> /// <param name="index"></param> /// <param name="value"></param> public void Insert(int index, object value) { if (index >= this.count) { throw new Exception(); } //插进去元素当空间不足时也是声明新的2倍长度数组,并拷贝旧数据。 //插进去数据道理是,将指定位置后的数据悉数后移,再将新数据放在指定位置。 int newCount = this.count + 1; if (this.array.Length < newCount) { object[] newArray = GetNewArray(); Array.Copy(this.array, newArray, index); this.array = newArray; } Array.Copy(this.array, index, this.array, index + 1, this.count - index); this.array[index] = value; this.count = newCount; } /// <summary> /// 检察当前鸠合是不是包括指定对象 /// </summary> /// <param name="value"></param> /// <returns></returns> public bool Contains(object value) { return this.IndexOf(value) >= 0; } /// <summary> /// 将鸠合的长度转变成现实长度 /// </summary> public void TrimToSize() { //为了消弭Add和Insert时增添的冗余,道理是新生成一个和现实长度雷同的数组,然后将值悉数移过来。 if (this.array.Length > this.count) { object[] newArray = null; if (this.count > 0) { newArray = new object[this.count]; Array.Copy(this.array, newArray, this.count); } else { newArray = new object[1]; } this.array = newArray; } } /// <summary> /// 清空鸠合 /// </summary> public void Clear() { this.count = 0; } /// <summary> /// 猎取鸠合的迭代器 /// </summary> /// <returns></returns> public IEnumerator GetEnumerator() { Enumertor enumerator = new Enumertor(this); return enumerator; } /// <summary> /// 转移鸠合元素 /// </summary> /// <param name="targetArray"></param> /// <param name="index"></param> public void CopyTo(Array targetArray, int index) { Array.Copy(this.array, 0, targetArray, index, this.count); } }
挪用测试:
static void Main(string[] args) { //挪用测试 ArrayList myArrayList = new ArrayList(); myArrayList.Add(40); myArrayList.Add(80); myArrayList.Add("Hello"); //运用for轮回遍历 for (int i = 0; i < myArrayList.Count; i++) { Console.WriteLine(myArrayList[i]); } Console.WriteLine("---------------------"); //运用迭代轮回 foreach (object obj in myArrayList) { Console.WriteLine(obj); } Console.WriteLine("---------------------"); myArrayList.Insert(1, "Insert"); foreach (object obj in myArrayList) { Console.WriteLine(obj); } Console.WriteLine("---------------------"); myArrayList.Remove("Insert"); foreach (object obj in myArrayList) { Console.WriteLine(obj); } Console.WriteLine("---------------------"); myArrayList.RemoveAt(1); foreach (object obj in myArrayList) { Console.WriteLine(obj); } Console.WriteLine("---------------------"); myArrayList.Clear(); foreach (object obj in myArrayList) { Console.WriteLine(obj); } Console.WriteLine("---------------------"); Random rand = new Random(); for (int i = 0; i < 10; i++) { myArrayList.Add(rand.Next(10)); } foreach (object obj in myArrayList) { Console.WriteLine(obj); } Console.WriteLine("---------------------"); Console.WriteLine("鸠合是不是包括为1的元素 ? " + (myArrayList.Contains(0) ? "包括" : "不包括")); Console.WriteLine("元素1的位置 " + myArrayList.IndexOf(1)); Console.ReadLine(); }
以上就是的内容,更多相关内容请关注ki4网(www.ki4.cn)!