C#基础知识整理 基础知识 IList接口——非泛型

 

C#基础知识整理 基础知识 IList接口——非泛型

本文章已收录于: 
 分类:

了解了ICollection接口、迭代以及泛型集合,下面再详细了解一下IList接口。
通过MSDN可以看到IList接口有两种:

元素为object类型的IList接口,可以放不同类型的对象引用;
IList<T>泛型接口,只能存放指定类型的对象引用。
其实,IList和IList<T>也称之为向量,特点是可以动态的改变集合的长度,无需确定集合的初始长度,集合会随着存放数据的数量自动变化。
可以看到IList和IList<T>的继承关系:

[csharp] view plain copy
 
 print?
  1. [ComVisibleAttribute(true)]  
  2. public interface IList : ICollection, IEnumerable  
  3.   
  4. public interface IList<T> : ICollection<T>,   
  5.  IEnumerable<T>, IEnumerable  

现在再返回去看下,IList和IList<T>的区别,看如下代码,ArrayList是继承IList的,List继承IList<T>:

[csharp] view plain copy
 
 print?
  1. public class IListClass  
  2.  {  
  3.      void test()  
  4.      {  
  5.          TestClass1 c1 = null;  
  6.   
  7.          ArrayList arryList = new ArrayList();  
  8.   
  9.          arryList.Add(c1);  
  10.   
  11.          List<TestClass1> list = new List<TestClass1>();  
  12.   
  13.          list.Add(c1);  
  14.   
  15.          //取值  
  16.          TestClass1 getC1Array = arryList[0] as TestClass1;//必须要一次强制转换  
  17.   
  18.          TestClass1 getC1List = list[0];//不需要转换,所谓泛型  
  19.      }  
  20.  }  
  21.   
  22.  public class TestClass1  
  23.  {  
  24.  }  

这下就比较明白了。
一、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命名空间下。
 四、实现代码(非泛型)
 

[csharp] view plain copy
 
 print?
  1. /// <summary>  
  2.     /// 实现IList,非泛型  
  3.     /// </summary>  
  4.     public class ArrayList : IList  
  5.     {  
  6.         /// <summary>  
  7.         /// 迭代  
  8.         /// </summary>  
  9.         public struct Enumertor : IEnumerator  
  10.         {  
  11.             /// <summary>  
  12.             /// 迭代索引  
  13.             /// </summary>  
  14.             private int index;  
  15.   
  16.             /// <summary>  
  17.             /// 迭代器所属的向量类对象的引用  
  18.             /// </summary>  
  19.             private ArrayList arrayList;  
  20.   
  21.             /// <summary>  
  22.             /// 构造函数  
  23.             /// </summary>  
  24.             /// <param name="arrayList">迭代器所属的集合类</param>  
  25.             public Enumertor(ArrayList arrayList)  
  26.             {  
  27.                 this.arrayList = arrayList;  
  28.   
  29.                 this.index = -1;  
  30.             }  
  31.   
  32.             /// <summary>  
  33.             /// 获取当前对象,根据index的值返回向量对应的对象引用  
  34.             /// </summary>  
  35.             public object Current  
  36.             {  
  37.                 get   
  38.                 {   
  39.                     return arrayList[index];   
  40.                 }  
  41.             }  
  42.   
  43.             /// <summary>  
  44.             /// 将迭代器指向下一个数据位置,通过改变index的值,加1或减1  
  45.             /// </summary>  
  46.             /// <returns></returns>  
  47.             public bool MoveNext()  
  48.             {  
  49.                 if (this.index < arrayList.Count)  
  50.                 {  
  51.                     ++this.index;  
  52.                 }  
  53.   
  54.                 return this.index < arrayList.Count;  
  55.             }  
  56.   
  57.             /// <summary>  
  58.             /// 迭代器回到起始位置,将index置为-1  
  59.             /// </summary>  
  60.             public void Reset()  
  61.             {  
  62.                 this.index = -1;  
  63.             }  
  64.         }  
  65.   
  66.         /// <summary>  
  67.         /// 保存集合的数组  
  68.         /// </summary>  
  69.         private object[] array = new object[1];  
  70.   
  71.         /// <summary>  
  72.         /// 当前集合的长度  
  73.         /// </summary>  
  74.         private int count;  
  75.   
  76.         /// <summary>  
  77.         /// 默认构造函数  
  78.         /// </summary>  
  79.         public ArrayList()  
  80.         {  
  81.   
  82.         }  
  83.   
  84.         /// <summary>  
  85.         /// 参数构造函数,通过参数指定内部数组长度,减少重新分配空间  
  86.         /// </summary>  
  87.         /// <param name="capacity"></param>  
  88.         public ArrayList(int capacity)  
  89.         {  
  90.             if (capacity < 0)  
  91.             {  
  92.                 throw new Exception();  
  93.             }  
  94.   
  95.             if (capacity == 0)  
  96.             {  
  97.                 capacity = 1;  
  98.             }  
  99.   
  100.             this.array = new object[capacity];  
  101.   
  102.             this.count = 0;  
  103.         }  
  104.   
  105.         public int Count  
  106.         {  
  107.             get  
  108.             {  
  109.                 return this.count;//该属性只读  
  110.             }  
  111.         }  
  112.   
  113.         /// <summary>  
  114.         /// 集合实际使用长度  
  115.         /// </summary>  
  116.         public int Capacity  
  117.         {  
  118.             get  
  119.             {  
  120.                 return this.array.Length;  
  121.             }  
  122.         }  
  123.   
  124.         /// <summary>  
  125.         /// 是否固定大小  
  126.         /// </summary>  
  127.         public bool IsFixedSize  
  128.         {  
  129.             get  
  130.             {  
  131.                 return false;  
  132.             }  
  133.         }  
  134.   
  135.         /// <summary>  
  136.         /// 是否只读集合  
  137.         /// </summary>  
  138.         public bool IsReadOnly  
  139.         {  
  140.             get  
  141.             {  
  142.                 return false;  
  143.             }  
  144.         }  
  145.         /// <summary>  
  146.         /// 是否同步,即是否支持多线程访问  
  147.         /// </summary>  
  148.         public bool IsSynchronized  
  149.         {  
  150.             get  
  151.             {  
  152.                 return false;  
  153.             }  
  154.         }  
  155.         /// <summary>  
  156.         /// 同步对象  
  157.         /// </summary>  
  158.         public object SyncRoot  
  159.         {  
  160.             get  
  161.             {  
  162.                 return null;  
  163.             }  
  164.         }  
  165.   
  166.         /// <summary>  
  167.         /// 当array长度不足时,重新分配新的长度足够的数组  
  168.         /// </summary>  
  169.         /// <returns></returns>  
  170.         private object[] GetNewArray()  
  171.         {  
  172.             return new object[(this.array.Length + 1) * 2];  
  173.         }  
  174.   
  175.         public int Add(object value)  
  176.         {  
  177.             int newCount = this.count + 1;  
  178.   
  179.             if (this.array.Length < newCount)//长度不足  
  180.             {  
  181.                 object[] newArray = GetNewArray();  
  182.   
  183.                 Array.Copy(this.array, newArray, this.count);  
  184.   
  185.                 this.array = newArray;//重新引用,指向新数组  
  186.             }  
  187.   
  188.             //增加新元素  
  189.             this.array[this.count] = value;  
  190.   
  191.             this.count = newCount;  
  192.   
  193.             //返回新元素的索引位置  
  194.             return this.count - 1;  
  195.         }  
  196.   
  197.         /// <summary>  
  198.         /// 索引器属性,按索引返回向量中的某一项  
  199.         /// </summary>  
  200.         /// <param name="index"></param>  
  201.         /// <returns></returns>  
  202.         public object this[int index]  
  203.         {  
  204.             get  
  205.             {  
  206.                 if (index < 0 || index >= this.count)  
  207.                 {  
  208.                     throw new Exception();  
  209.                 }  
  210.   
  211.                 return this.array[index];  
  212.             }  
  213.   
  214.             set  
  215.             {  
  216.                 if (index < 0 || index >= this.count)  
  217.                 {  
  218.                     throw new Exception();  
  219.                 }  
  220.   
  221.                 this.array[index] = value;  
  222.             }  
  223.         }  
  224.   
  225.         /// <summary>  
  226.         /// 删除集合中的元素  
  227.         /// </summary>  
  228.         /// <param name="index"></param>  
  229.         /// <param name="count"></param>  
  230.         public void RemoveRange(int index, int count)  
  231.         {  
  232.             if (index < 0)  
  233.             {  
  234.                 throw new Exception();  
  235.             }  
  236.   
  237.             int removeIndex = index + count;//计算集合中最后一个被删元素的索引  
  238.   
  239.             if (count < 0 || removeIndex > this.count)  
  240.             {  
  241.                 throw new Exception();  
  242.             }  
  243.   
  244.             //删除其实是将要删除元素之后的所有元素拷贝到要删除元素的位置覆盖掉  
  245.             Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);  
  246.   
  247.             //重新设置集合长度  
  248.             this.count -= count;  
  249.         }  
  250.   
  251.         /// <summary>  
  252.         /// 查找对应的数组项,实际是遍历查找  
  253.         /// </summary>  
  254.         /// <param name="value"></param>  
  255.         /// <returns></returns>  
  256.         public int IndexOf(object value)  
  257.         {  
  258.             int index = 0;  
  259.   
  260.             if (value == null)  
  261.             {  
  262.                 while (index < this.count)  
  263.                 {  
  264.                     if (this.array[index] == null)  
  265.                     {  
  266.                         return index;  
  267.                     }  
  268.   
  269.                     ++index;  
  270.                 }  
  271.             }  
  272.             else  
  273.             {  
  274.                 while (index < this.count)  
  275.                 {  
  276.                     if (this.array[index].Equals(value))  
  277.                     {  
  278.                         return index;  
  279.                     }  
  280.   
  281.                     ++index;  
  282.                 }  
  283.             }  
  284.   
  285.             return -1;  
  286.         }  
  287.   
  288.         /// <summary>  
  289.         /// 从集合中删除指定元素  
  290.         /// </summary>  
  291.         /// <param name="value"></param>  
  292.         public void Remove(object value)  
  293.         {  
  294.             int index = this.IndexOf(value);  
  295.   
  296.             if (index >= 0)  
  297.             {  
  298.                 this.RemoveRange(index, 1);  
  299.             }  
  300.         }  
  301.   
  302.         /// <summary>  
  303.         /// 从集合中删除指定位置的元素  
  304.         /// </summary>  
  305.         /// <param name="index"></param>  
  306.         public void RemoveAt(int index)  
  307.         {  
  308.             RemoveRange(index, 1);  
  309.         }  
  310.   
  311.         /// <summary>  
  312.         /// 获取最后一个元素的引用后删除最后一个元素  
  313.         /// </summary>  
  314.         /// <returns></returns>  
  315.         public object PopBack()  
  316.         {  
  317.             object obj = this.array[this.count - 1];  
  318.   
  319.             RemoveAt(this.count - 1);  
  320.   
  321.             return obj;  
  322.         }  
  323.   
  324.         /// <summary>  
  325.         /// 获取第一个元素引用并删除第一个元素  
  326.         /// </summary>  
  327.         /// <returns></returns>  
  328.         public object PropFront()  
  329.         {  
  330.             object obj = this.array[0];  
  331.   
  332.             RemoveAt(0);  
  333.   
  334.             return obj;  
  335.         }  
  336.   
  337.         /// <summary>  
  338.         /// 插入元素  
  339.         /// </summary>  
  340.         /// <param name="index"></param>  
  341.         /// <param name="value"></param>  
  342.         public void Insert(int index, object value)  
  343.         {  
  344.             if (index >= this.count)  
  345.             {  
  346.                 throw new Exception();  
  347.             }  
  348.             //插入元素当空间不足时也是声明新的2倍长度数组,并拷贝旧数据。  
  349.             //插入数据原理是,将指定位置后的数据全部后移,再将新数据放在指定位置。  
  350.   
  351.             int newCount = this.count + 1;  
  352.   
  353.             if (this.array.Length < newCount)  
  354.             {  
  355.                 object[] newArray = GetNewArray();  
  356.   
  357.                 Array.Copy(this.array, newArray, index);  
  358.   
  359.                 this.array = newArray;  
  360.             }  
  361.   
  362.             Array.Copy(this.array, index, this.array, index + 1, this.count - index);  
  363.   
  364.             this.array[index] = value;  
  365.   
  366.             this.count = newCount;  
  367.         }  
  368.   
  369.         /// <summary>  
  370.         /// 查看当前集合是否包含指定对象  
  371.         /// </summary>  
  372.         /// <param name="value"></param>  
  373.         /// <returns></returns>  
  374.         public bool Contains(object value)  
  375.         {  
  376.             return this.IndexOf(value) >= 0;  
  377.         }  
  378.   
  379.         /// <summary>  
  380.         /// 将集合的长度改变为实际长度  
  381.         /// </summary>  
  382.         public void TrimToSize()  
  383.         {  
  384.             //为了消除Add和Insert时增加的冗余,原理是新生成一个和实际长度相同的数组,然后将值全部移过来。  
  385.             if (this.array.Length > this.count)  
  386.             {  
  387.                 object[] newArray = null;  
  388.   
  389.                 if (this.count > 0)  
  390.                 {  
  391.                     newArray = new object[this.count];  
  392.   
  393.                     Array.Copy(this.array, newArray, this.count);  
  394.                 }  
  395.                 else  
  396.                 {  
  397.                     newArray = new object[1];  
  398.                 }  
  399.   
  400.                 this.array = newArray;  
  401.             }  
  402.         }  
  403.   
  404.         /// <summary>  
  405.         /// 清空集合  
  406.         /// </summary>  
  407.         public void Clear()  
  408.         {  
  409.             this.count = 0;  
  410.         }  
  411.         /// <summary>  
  412.         /// 获取集合的迭代器  
  413.         /// </summary>  
  414.         /// <returns></returns>  
  415.         public IEnumerator GetEnumerator()  
  416.         {  
  417.             Enumertor enumerator = new Enumertor(this);  
  418.   
  419.             return enumerator;  
  420.         }  
  421.   
  422.         /// <summary>  
  423.         /// 转移集合元素  
  424.         /// </summary>  
  425.         /// <param name="targetArray"></param>  
  426.         /// <param name="index"></param>  
  427.         public void CopyTo(Array targetArray, int index)  
  428.         {  
  429.             Array.Copy(this.array, 0, targetArray, index, this.count);  
  430.         }  
  431.     }  


调用测试

[csharp] view plain copy
 
 print?
  1. static void Main(string[] args)  
  2.         {  
  3.             //调用测试  
  4.   
  5.             ArrayList myArrayList = new ArrayList();  
  6.   
  7.             myArrayList.Add(40);  
  8.   
  9.             myArrayList.Add(80);  
  10.   
  11.             myArrayList.Add("Hello");  
  12.   
  13.             //使用for循环遍历  
  14.             for (int i = 0; i < myArrayList.Count; i++)  
  15.             {  
  16.                 Console.WriteLine(myArrayList[i]);  
  17.             }  
  18.   
  19.             Console.WriteLine("---------------------");  
  20.   
  21.             //使用迭代循环  
  22.             foreach (object obj in myArrayList)  
  23.             {  
  24.                 Console.WriteLine(obj);  
  25.             }  
  26.   
  27.             Console.WriteLine("---------------------");  
  28.   
  29.             myArrayList.Insert(1, "Insert");  
  30.   
  31.             foreach (object obj in myArrayList)  
  32.             {  
  33.                 Console.WriteLine(obj);  
  34.             }  
  35.   
  36.             Console.WriteLine("---------------------");  
  37.   
  38.             myArrayList.Remove("Insert");  
  39.   
  40.             foreach (object obj in myArrayList)  
  41.             {  
  42.                 Console.WriteLine(obj);  
  43.             }  
  44.   
  45.             Console.WriteLine("---------------------");  
  46.   
  47.             myArrayList.RemoveAt(1);  
  48.   
  49.             foreach (object obj in myArrayList)  
  50.             {  
  51.                 Console.WriteLine(obj);  
  52.             }  
  53.   
  54.             Console.WriteLine("---------------------");  
  55.   
  56.             myArrayList.Clear();  
  57.   
  58.             foreach (object obj in myArrayList)  
  59.             {  
  60.                 Console.WriteLine(obj);  
  61.             }  
  62.   
  63.             Console.WriteLine("---------------------");  
  64.   
  65.             Random rand = new Random();  
  66.   
  67.             for (int i = 0; i < 10; i++)  
  68.             {  
  69.                 myArrayList.Add(rand.Next(10));  
  70.             }  
  71.   
  72.             foreach (object obj in myArrayList)  
  73.             {  
  74.                 Console.WriteLine(obj);  
  75.             }  
  76.   
  77.             Console.WriteLine("---------------------");  
  78.   
  79.             Console.WriteLine("集合是否包含为1的元素 ? " + (myArrayList.Contains(0) ? "包含" : "不包含"));  
  80.   
  81.             Console.WriteLine("元素1的位置   " + myArrayList.IndexOf(1));  
  82.   
  83.             Console.ReadLine();  
  84.         }  

结果:

代码下载:http://download.csdn.net/detail/yysyangyangyangshan/4686479

 

C#基础知识整理 基础知识(16) IList接口——非泛型

 3914人阅读 评论(1) 收藏 举报
本文章已收录于: 
 分类:

了解了ICollection接口、迭代以及泛型集合,下面再详细了解一下IList接口。
通过MSDN可以看到IList接口有两种:

元素为object类型的IList接口,可以放不同类型的对象引用;
IList<T>泛型接口,只能存放指定类型的对象引用。
其实,IList和IList<T>也称之为向量,特点是可以动态的改变集合的长度,无需确定集合的初始长度,集合会随着存放数据的数量自动变化。
可以看到IList和IList<T>的继承关系:

[csharp] view plain copy
 
 print?
  1. [ComVisibleAttribute(true)]  
  2. public interface IList : ICollection, IEnumerable  
  3.   
  4. public interface IList<T> : ICollection<T>,   
  5.  IEnumerable<T>, IEnumerable  

现在再返回去看下,IList和IList<T>的区别,看如下代码,ArrayList是继承IList的,List继承IList<T>:

[csharp] view plain copy
 
 print?
  1. public class IListClass  
  2.  {  
  3.      void test()  
  4.      {  
  5.          TestClass1 c1 = null;  
  6.   
  7.          ArrayList arryList = new ArrayList();  
  8.   
  9.          arryList.Add(c1);  
  10.   
  11.          List<TestClass1> list = new List<TestClass1>();  
  12.   
  13.          list.Add(c1);  
  14.   
  15.          //取值  
  16.          TestClass1 getC1Array = arryList[0] as TestClass1;//必须要一次强制转换  
  17.   
  18.          TestClass1 getC1List = list[0];//不需要转换,所谓泛型  
  19.      }  
  20.  }  
  21.   
  22.  public class TestClass1  
  23.  {  
  24.  }  

这下就比较明白了。
一、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命名空间下。
 四、实现代码(非泛型)
 

[csharp] view plain copy
 
 print?
  1. /// <summary>  
  2.     /// 实现IList,非泛型  
  3.     /// </summary>  
  4.     public class ArrayList : IList  
  5.     {  
  6.         /// <summary>  
  7.         /// 迭代  
  8.         /// </summary>  
  9.         public struct Enumertor : IEnumerator  
  10.         {  
  11.             /// <summary>  
  12.             /// 迭代索引  
  13.             /// </summary>  
  14.             private int index;  
  15.   
  16.             /// <summary>  
  17.             /// 迭代器所属的向量类对象的引用  
  18.             /// </summary>  
  19.             private ArrayList arrayList;  
  20.   
  21.             /// <summary>  
  22.             /// 构造函数  
  23.             /// </summary>  
  24.             /// <param name="arrayList">迭代器所属的集合类</param>  
  25.             public Enumertor(ArrayList arrayList)  
  26.             {  
  27.                 this.arrayList = arrayList;  
  28.   
  29.                 this.index = -1;  
  30.             }  
  31.   
  32.             /// <summary>  
  33.             /// 获取当前对象,根据index的值返回向量对应的对象引用  
  34.             /// </summary>  
  35.             public object Current  
  36.             {  
  37.                 get   
  38.                 {   
  39.                     return arrayList[index];   
  40.                 }  
  41.             }  
  42.   
  43.             /// <summary>  
  44.             /// 将迭代器指向下一个数据位置,通过改变index的值,加1或减1  
  45.             /// </summary>  
  46.             /// <returns></returns>  
  47.             public bool MoveNext()  
  48.             {  
  49.                 if (this.index < arrayList.Count)  
  50.                 {  
  51.                     ++this.index;  
  52.                 }  
  53.   
  54.                 return this.index < arrayList.Count;  
  55.             }  
  56.   
  57.             /// <summary>  
  58.             /// 迭代器回到起始位置,将index置为-1  
  59.             /// </summary>  
  60.             public void Reset()  
  61.             {  
  62.                 this.index = -1;  
  63.             }  
  64.         }  
  65.   
  66.         /// <summary>  
  67.         /// 保存集合的数组  
  68.         /// </summary>  
  69.         private object[] array = new object[1];  
  70.   
  71.         /// <summary>  
  72.         /// 当前集合的长度  
  73.         /// </summary>  
  74.         private int count;  
  75.   
  76.         /// <summary>  
  77.         /// 默认构造函数  
  78.         /// </summary>  
  79.         public ArrayList()  
  80.         {  
  81.   
  82.         }  
  83.   
  84.         /// <summary>  
  85.         /// 参数构造函数,通过参数指定内部数组长度,减少重新分配空间  
  86.         /// </summary>  
  87.         /// <param name="capacity"></param>  
  88.         public ArrayList(int capacity)  
  89.         {  
  90.             if (capacity < 0)  
  91.             {  
  92.                 throw new Exception();  
  93.             }  
  94.   
  95.             if (capacity == 0)  
  96.             {  
  97.                 capacity = 1;  
  98.             }  
  99.   
  100.             this.array = new object[capacity];  
  101.   
  102.             this.count = 0;  
  103.         }  
  104.   
  105.         public int Count  
  106.         {  
  107.             get  
  108.             {  
  109.                 return this.count;//该属性只读  
  110.             }  
  111.         }  
  112.   
  113.         /// <summary>  
  114.         /// 集合实际使用长度  
  115.         /// </summary>  
  116.         public int Capacity  
  117.         {  
  118.             get  
  119.             {  
  120.                 return this.array.Length;  
  121.             }  
  122.         }  
  123.   
  124.         /// <summary>  
  125.         /// 是否固定大小  
  126.         /// </summary>  
  127.         public bool IsFixedSize  
  128.         {  
  129.             get  
  130.             {  
  131.                 return false;  
  132.             }  
  133.         }  
  134.   
  135.         /// <summary>  
  136.         /// 是否只读集合  
  137.         /// </summary>  
  138.         public bool IsReadOnly  
  139.         {  
  140.             get  
  141.             {  
  142.                 return false;  
  143.             }  
  144.         }  
  145.         /// <summary>  
  146.         /// 是否同步,即是否支持多线程访问  
  147.         /// </summary>  
  148.         public bool IsSynchronized  
  149.         {  
  150.             get  
  151.             {  
  152.                 return false;  
  153.             }  
  154.         }  
  155.         /// <summary>  
  156.         /// 同步对象  
  157.         /// </summary>  
  158.         public object SyncRoot  
  159.         {  
  160.             get  
  161.             {  
  162.                 return null;  
  163.             }  
  164.         }  
  165.   
  166.         /// <summary>  
  167.         /// 当array长度不足时,重新分配新的长度足够的数组  
  168.         /// </summary>  
  169.         /// <returns></returns>  
  170.         private object[] GetNewArray()  
  171.         {  
  172.             return new object[(this.array.Length + 1) * 2];  
  173.         }  
  174.   
  175.         public int Add(object value)  
  176.         {  
  177.             int newCount = this.count + 1;  
  178.   
  179.             if (this.array.Length < newCount)//长度不足  
  180.             {  
  181.                 object[] newArray = GetNewArray();  
  182.   
  183.                 Array.Copy(this.array, newArray, this.count);  
  184.   
  185.                 this.array = newArray;//重新引用,指向新数组  
  186.             }  
  187.   
  188.             //增加新元素  
  189.             this.array[this.count] = value;  
  190.   
  191.             this.count = newCount;  
  192.   
  193.             //返回新元素的索引位置  
  194.             return this.count - 1;  
  195.         }  
  196.   
  197.         /// <summary>  
  198.         /// 索引器属性,按索引返回向量中的某一项  
  199.         /// </summary>  
  200.         /// <param name="index"></param>  
  201.         /// <returns></returns>  
  202.         public object this[int index]  
  203.         {  
  204.             get  
  205.             {  
  206.                 if (index < 0 || index >= this.count)  
  207.                 {  
  208.                     throw new Exception();  
  209.                 }  
  210.   
  211.                 return this.array[index];  
  212.             }  
  213.   
  214.             set  
  215.             {  
  216.                 if (index < 0 || index >= this.count)  
  217.                 {  
  218.                     throw new Exception();  
  219.                 }  
  220.   
  221.                 this.array[index] = value;  
  222.             }  
  223.         }  
  224.   
  225.         /// <summary>  
  226.         /// 删除集合中的元素  
  227.         /// </summary>  
  228.         /// <param name="index"></param>  
  229.         /// <param name="count"></param>  
  230.         public void RemoveRange(int index, int count)  
  231.         {  
  232.             if (index < 0)  
  233.             {  
  234.                 throw new Exception();  
  235.             }  
  236.   
  237.             int removeIndex = index + count;//计算集合中最后一个被删元素的索引  
  238.   
  239.             if (count < 0 || removeIndex > this.count)  
  240.             {  
  241.                 throw new Exception();  
  242.             }  
  243.   
  244.             //删除其实是将要删除元素之后的所有元素拷贝到要删除元素的位置覆盖掉  
  245.             Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);  
  246.   
  247.             //重新设置集合长度  
  248.             this.count -= count;  
  249.         }  
  250.   
  251.         /// <summary>  
  252.         /// 查找对应的数组项,实际是遍历查找  
  253.         /// </summary>  
  254.         /// <param name="value"></param>  
  255.         /// <returns></returns>  
  256.         public int IndexOf(object value)  
  257.         {  
  258.             int index = 0;  
  259.   
  260.             if (value == null)  
  261.             {  
  262.                 while (index < this.count)  
  263.                 {  
  264.                     if (this.array[index] == null)  
  265.                     {  
  266.                         return index;  
  267.                     }  
  268.   
  269.                     ++index;  
  270.                 }  
  271.             }  
  272.             else  
  273.             {  
  274.                 while (index < this.count)  
  275.                 {  
  276.                     if (this.array[index].Equals(value))  
  277.                     {  
  278.                         return index;  
  279.                     }  
  280.   
  281.                     ++index;  
  282.                 }  
  283.             }  
  284.   
  285.             return -1;  
  286.         }  
  287.   
  288.         /// <summary>  
  289.         /// 从集合中删除指定元素  
  290.         /// </summary>  
  291.         /// <param name="value"></param>  
  292.         public void Remove(object value)  
  293.         {  
  294.             int index = this.IndexOf(value);  
  295.   
  296.             if (index >= 0)  
  297.             {  
  298.                 this.RemoveRange(index, 1);  
  299.             }  
  300.         }  
  301.   
  302.         /// <summary>  
  303.         /// 从集合中删除指定位置的元素  
  304.         /// </summary>  
  305.         /// <param name="index"></param>  
  306.         public void RemoveAt(int index)  
  307.         {  
  308.             RemoveRange(index, 1);  
  309.         }  
  310.   
  311.         /// <summary>  
  312.         /// 获取最后一个元素的引用后删除最后一个元素  
  313.         /// </summary>  
  314.         /// <returns></returns>  
  315.         public object PopBack()  
  316.         {  
  317.             object obj = this.array[this.count - 1];  
  318.   
  319.             RemoveAt(this.count - 1);  
  320.   
  321.             return obj;  
  322.         }  
  323.   
  324.         /// <summary>  
  325.         /// 获取第一个元素引用并删除第一个元素  
  326.         /// </summary>  
  327.         /// <returns></returns>  
  328.         public object PropFront()  
  329.         {  
  330.             object obj = this.array[0];  
  331.   
  332.             RemoveAt(0);  
  333.   
  334.             return obj;  
  335.         }  
  336.   
  337.         /// <summary>  
  338.         /// 插入元素  
  339.         /// </summary>  
  340.         /// <param name="index"></param>  
  341.         /// <param name="value"></param>  
  342.         public void Insert(int index, object value)  
  343.         {  
  344.             if (index >= this.count)  
  345.             {  
  346.                 throw new Exception();  
  347.             }  
  348.             //插入元素当空间不足时也是声明新的2倍长度数组,并拷贝旧数据。  
  349.             //插入数据原理是,将指定位置后的数据全部后移,再将新数据放在指定位置。  
  350.   
  351.             int newCount = this.count + 1;  
  352.   
  353.             if (this.array.Length < newCount)  
  354.             {  
  355.                 object[] newArray = GetNewArray();  
  356.   
  357.                 Array.Copy(this.array, newArray, index);  
  358.   
  359.                 this.array = newArray;  
  360.             }  
  361.   
  362.             Array.Copy(this.array, index, this.array, index + 1, this.count - index);  
  363.   
  364.             this.array[index] = value;  
  365.   
  366.             this.count = newCount;  
  367.         }  
  368.   
  369.         /// <summary>  
  370.         /// 查看当前集合是否包含指定对象  
  371.         /// </summary>  
  372.         /// <param name="value"></param>  
  373.         /// <returns></returns>  
  374.         public bool Contains(object value)  
  375.         {  
  376.             return this.IndexOf(value) >= 0;  
  377.         }  
  378.   
  379.         /// <summary>  
  380.         /// 将集合的长度改变为实际长度  
  381.         /// </summary>  
  382.         public void TrimToSize()  
  383.         {  
  384.             //为了消除Add和Insert时增加的冗余,原理是新生成一个和实际长度相同的数组,然后将值全部移过来。  
  385.             if (this.array.Length > this.count)  
  386.             {  
  387.                 object[] newArray = null;  
  388.   
  389.                 if (this.count > 0)  
  390.                 {  
  391.                     newArray = new object[this.count];  
  392.   
  393.                     Array.Copy(this.array, newArray, this.count);  
  394.                 }  
  395.                 else  
  396.                 {  
  397.                     newArray = new object[1];  
  398.                 }  
  399.   
  400.                 this.array = newArray;  
  401.             }  
  402.         }  
  403.   
  404.         /// <summary>  
  405.         /// 清空集合  
  406.         /// </summary>  
  407.         public void Clear()  
  408.         {  
  409.             this.count = 0;  
  410.         }  
  411.         /// <summary>  
  412.         /// 获取集合的迭代器  
  413.         /// </summary>  
  414.         /// <returns></returns>  
  415.         public IEnumerator GetEnumerator()  
  416.         {  
  417.             Enumertor enumerator = new Enumertor(this);  
  418.   
  419.             return enumerator;  
  420.         }  
  421.   
  422.         /// <summary>  
  423.         /// 转移集合元素  
  424.         /// </summary>  
  425.         /// <param name="targetArray"></param>  
  426.         /// <param name="index"></param>  
  427.         public void CopyTo(Array targetArray, int index)  
  428.         {  
  429.             Array.Copy(this.array, 0, targetArray, index, this.count);  
  430.         }  
  431.     }  


调用测试

[csharp] view plain copy
 
 print?
  1. static void Main(string[] args)  
  2.         {  
  3.             //调用测试  
  4.   
  5.             ArrayList myArrayList = new ArrayList();  
  6.   
  7.             myArrayList.Add(40);  
  8.   
  9.             myArrayList.Add(80);  
  10.   
  11.             myArrayList.Add("Hello");  
  12.   
  13.             //使用for循环遍历  
  14.             for (int i = 0; i < myArrayList.Count; i++)  
  15.             {  
  16.                 Console.WriteLine(myArrayList[i]);  
  17.             }  
  18.   
  19.             Console.WriteLine("---------------------");  
  20.   
  21.             //使用迭代循环  
  22.             foreach (object obj in myArrayList)  
  23.             {  
  24.                 Console.WriteLine(obj);  
  25.             }  
  26.   
  27.             Console.WriteLine("---------------------");  
  28.   
  29.             myArrayList.Insert(1, "Insert");  
  30.   
  31.             foreach (object obj in myArrayList)  
  32.             {  
  33.                 Console.WriteLine(obj);  
  34.             }  
  35.   
  36.             Console.WriteLine("---------------------");  
  37.   
  38.             myArrayList.Remove("Insert");  
  39.   
  40.             foreach (object obj in myArrayList)  
  41.             {  
  42.                 Console.WriteLine(obj);  
  43.             }  
  44.   
  45.             Console.WriteLine("---------------------");  
  46.   
  47.             myArrayList.RemoveAt(1);  
  48.   
  49.             foreach (object obj in myArrayList)  
  50.             {  
  51.                 Console.WriteLine(obj);  
  52.             }  
  53.   
  54.             Console.WriteLine("---------------------");  
  55.   
  56.             myArrayList.Clear();  
  57.   
  58.             foreach (object obj in myArrayList)  
  59.             {  
  60.                 Console.WriteLine(obj);  
  61.             }  
  62.   
  63.             Console.WriteLine("---------------------");  
  64.   
  65.             Random rand = new Random();  
  66.   
  67.             for (int i = 0; i < 10; i++)  
  68.             {  
  69.                 myArrayList.Add(rand.Next(10));  
  70.             }  
  71.   
  72.             foreach (object obj in myArrayList)  
  73.             {  
  74.                 Console.WriteLine(obj);  
  75.             }  
  76.   
  77.             Console.WriteLine("---------------------");  
  78.   
  79.             Console.WriteLine("集合是否包含为1的元素 ? " + (myArrayList.Contains(0) ? "包含" : "不包含"));  
  80.   
  81.             Console.WriteLine("元素1的位置   " + myArrayList.IndexOf(1));  
  82.   
  83.             Console.ReadLine();  
  84.         }  

结果:

代码下载:http://download.csdn.net/detail/yysyangyangyangshan/4686479

原文地址:https://www.cnblogs.com/jingxuan-li/p/6863152.html