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

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

原文链接 https://blog.csdn.net/imxiangzi/article/details/80853960

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

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


  
  
  1. [ ComVisibleAttribute(true)]
  2. public interface IList : ICollection, IEnumerable
  3. public interface IList< T> : ICollection< T>,
  4. IEnumerable< T>, IEnumerable

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


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

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


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


调用测试:

 


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

结果:

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


FROM: https://blog.csdn.net/yysyangyangyangshan/article/details/8111669



猜你喜欢

转载自www.cnblogs.com/sunny3158/p/11944910.html