A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨进 中级黑马   /  2012-9-23 23:44  /  1858 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨进 于 2012-9-24 01:18 编辑

C#菜鸟一只,C#中数组一旦定义长度就不能改变,一直很困扰!今天终于知道还有动态数组ArrayList这种东东 ,百度文库上搜到了一篇文章介绍得很详细。给大家分享下,最好能蹭点技术分{:soso_e144:}
  1. System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。
  2. 一、优点
  3. 1. 支持自动改变大小的功能
  4. 2. 可以灵活的插入元素
  5. 3. 可以灵活的删除元素
  6. 4. 可以灵活访问元素
  7. 二、局限性
  8. 跟一般的数组比起来,速度上差些
  9. 三、添加元素
  10. 1.public virtual int Add(object value);
  11. 将对象添加到ArrayList的结尾处
  12. ArrayList aList=new ArrayList();
  13. aList.Add("a");
  14. aList.Add("b");
  15. aList.Add("c");
  16. aList.Add("d");
  17. aList.Add("e");
  18. 内容为abcde
  19. 2.public virtual void Insert(int index,object value);
  20. 将元素插入ArrayList的指定索引处
  21. ArrayList aList=new ArrayList();
  22. aList.Add("a");
  23. aList.Add("b");
  24. aList.Add("c");
  25. aList.Add("d");
  26. aList.Add("e");
  27. aList.Insert(0,"aa");
  28. 结果为aaabcde
  29. 3.public virtual void InsertRange(int index,ICollectionc);
  30. 将集合中的某个元素插入ArrayList的指定索引处
  31. ArrayList aList=new ArrayList();
  32. aList.Add("a");
  33. aList.Add("b");
  34. aList.Add("c");
  35. aList.Add("d");
  36. aList.Add("e");
  37. ArrayList list2=new ArrayList();
  38. list2.Add("tt");
  39. list2.Add("ttt");
  40. aList.InsertRange(2,list2);
  41. 结果为abtttttcde
  42. 四、删除
  43. a)public virtual void Remove(object obj);
  44. 从ArrayList中移除特定对象的第一个匹配项,注意是第一个
  45. ArrayList aList=new ArrayList();
  46. aList.Add("a");
  47. aList.Add("b");
  48. aList.Add("c");
  49. aList.Add("d");
  50. aList.Add("e");
  51. aList.Remove("a");
  52. 结果为bcde
  53. 2.public virtual void RemoveAt(intindex);
  54. 移除ArrayList的指定索引处的元素
  55. aList.Add("a");
  56. aList.Add("b");
  57. aList.Add("c");
  58. aList.Add("d");
  59. aList.Add("e");
  60. aList.RemoveAt(0);
  61. 结果为bcde
  62. 3.public virtual void RemoveRange(int index,int count);
  63. 从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目
  64. aList.Add("a");
  65. aList.Add("b");
  66. aList.Add("c");
  67. aList.Add("d");
  68. aList.Add("e");
  69. aList.RemoveRange(1,3);
  70. 结果为ae
  71. 4.public virtual void Clear();
  72. 从ArrayList中移除所有元素。
  73. 五、排序
  74. a)public virtual void Sort();
  75. 对ArrayList或它的一部分中的元素进行排序。
  76. ArrayListaList=newArrayList();
  77. aList.Add("e");
  78. aList.Add("a");
  79. aList.Add("b");
  80. aList.Add("c");
  81. aList.Add("d");
  82. DropDownList1.DataSource=aList;//DropDown ListDropDownList1;
  83. DropDownList1.DataBind();
  84. 结果为eabcd
  85. ArrayList aList=new ArrayList();
  86. aList.Add("a");
  87. aList.Add("b");
  88. aList.Add("c");
  89. aList.Add("d");
  90. aList.Add("e");
  91. aList.Sort();//排序
  92. DropDownList1.DataSource=aList;//DropDownListDropDownList1;
  93. DropDownList1.DataBind();
  94. 结果为abcde
  95. b)public virtual void Reverse();
  96. 将ArrayList或它的一部分中元素的顺序反转。
  97. ArrayList aList=new ArrayList();
  98. aList.Add("a");
  99. aList.Add("b");
  100. aList.Add("c");
  101. aList.Add("d");
  102. aList.Add("e");
  103. aList.Reverse();//反转
  104. DropDownList1.DataSource=aList;//DropDownListDropDownList1;
  105. DropDownList1.DataBind();
  106. 结果为edcba
  107. 六、查找
  108. a)public virtual int IndexOf(object);
  109. b)public virtual int IndexOf(object,int);
  110. c)public virtual int IndexOf(object,int,int);
  111. 返回ArrayList或它的一部分中某个值的第一个匹配项的从零开始的索引。没找到返回-1。
  112. ArrayList aList=new ArrayList();
  113. aList.Add("a");
  114. aList.Add("b");
  115. aList.Add("c");
  116. aList.Add("d");
  117. aList.Add("e");
  118. intnIndex=aList.IndexOf(“a”);//1
  119. nIndex=aList.IndexOf(“p”);//没找到,-1
  120. d)public virtual int LastIndexOf(object);
  121. e)public virtual int LastIndexOf(object,int);
  122. f)public virtual int LastIndexOf(object,int,int);
  123. 返回ArrayList或它的一部分中某个值的最后一个匹配项的从零开始的索引。
  124. ArrayList aList=new ArrayList();
  125. aList.Add("a");
  126. aList.Add("b");
  127. aList.Add("a");//同0
  128. aList.Add("d");
  129. aList.Add("e");
  130. intnIndex=aList.LastIndexOf("a");//值为2而不是0
  131. g)public virtual bool Contains(objectitem);
  132. 确定某个元素是否在ArrayList中。包含返回true,否则返回false
  133. 七、获取数组中的元素
  134. 下面以整数为例,给出获取某个元素的值的方法
  135. ArrayList aList=new ArrayList();
  136. for(int i=0;i<10;i++)
  137.      aList.Add(i);
  138. for(i=0;i<10;i++)
  139.     Textbox1.text+=(int)aList[i]+" ";//获取的方式基本与一般的数组相同,使用下标的方式进行
  140. 结果为:0 1 2 3 4 5 6 7 8 9
  141. 八、其他
  142. 1.public virtual intCapacity{get;set;}
  143. 获取或设置ArrayList可包含的元素数。
  144. 2.public virtual intCount{get;}
  145. 获取ArrayList中实际包含的元素数。
  146. Capacity是ArrayList可以存储的元素数。Count是ArrayList中实际包含的元素数。Capacity总是大于或等于Count。如果在添加元素时,Count超过Capacity,则该列表的容量会通过自动重新分配内部数组加倍。
  147. 如果Capacity的值显式设置,则内部数组也需要重新分配以容纳指定的容量。如果Capacity被显式设置为0,则公共语言运行库将其设置为默认容量。默认容量为16。
  148. 在调用Clear后,Count为0,而此时Capacity切是默认容量16,而不是0
  149. 3.public virtual void TrimToSize();
  150. 将容量设置为ArrayList中元素的实际数量。
  151. 如果不向列表中添加新元素,则此方法可用于最小化列表的内存系统开销。
  152. 若要完全清除列表中的所有元素,请在调用TrimToSize之前调用Clear方法。截去空ArrayList会将ArrayList的容量设置为默认容量,而不是零。
  153. ArrayList aList=new ArrayList();
  154. aList.Add("a");
  155. aList.Add("b");
  156. aList.Add("c");
  157. aList.Add("d");
  158. aList.Add("e");//Count=5,Capacity=16,
  159. aList.TrimToSize();//Count=Capacity=5;
复制代码

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

4 个回复

正序浏览
李阳_TickTock 发表于 2012-9-24 00:13
数组是固定大小的,不能伸缩。System.Array.Resize这个泛型方法可以重置数组大小,
该方法是重新创建新设 ...

学习了,谢谢
回复 使用道具 举报
本帖最后由 李阳_TickTock 于 2012-9-24 00:16 编辑

数组是固定大小的,不能伸缩。System.Array.Resize这个泛型方法可以重置数组大小,
该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃

和ArrayList一样可以伸缩的还有Dictionary和Hashtable
如果你看过基础视频中的套接字聊天室,就会发现其中对Dictionary的应用,非常强大的一个东西,定义好key和value,你几乎可以用它做任何事
  1. //定义一个数组存放套接字
  2.         Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
  3.         //定义一个数组存放线程
  4.         Dictionary<string, Thread> dictThread = new Dictionary<string, Thread>();
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 杨进 于 2012-9-24 00:04 编辑
廖创发 发表于 2012-9-23 23:48
一看到Arraylist就好激动,以为终于可以搞点技术方,原来已经是已解决,白高兴 ...
{:soso_e100:}
回复 使用道具 举报
一看到Arraylist就好激动,以为终于可以搞点技术方{:soso_e102:},原来已经是已解决{:soso_e105:},白高兴一场
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马