{"id":697,"date":"2009-10-29T10:33:00","date_gmt":"2009-10-29T02:33:00","guid":{"rendered":""},"modified":"2013-11-17T12:39:53","modified_gmt":"2013-11-17T04:39:53","slug":"c-arraylist%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/697.html","title":{"rendered":"C# ArrayList\u7c7b"},"content":{"rendered":"<p>System.Collections.ArrayList\u7c7b\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u6570\u7ec4\u3002\u901a\u8fc7\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u5c31\u53ef\u4ee5\u52a8\u6001\u6539\u53d8\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u4e00\uff0e\u4f18\u70b9<\/p>\n<p>1\u3002\u652f\u6301\u81ea\u52a8\u6539\u53d8\u5927\u5c0f\u7684\u529f\u80fd<br \/>\n2\u3002\u53ef\u4ee5\u7075\u6d3b\u7684\u63d2\u5165\u5143\u7d20<br \/>\n3\u3002\u53ef\u4ee5\u7075\u6d3b\u7684\u5220\u9664\u5143\u7d20<\/p>\n<p>\u4e8c\uff0e\u5c40\u9650\u6027<\/p>\n<p>\u8ddf\u4e00\u822c\u7684\u6570\u7ec4\u6bd4\u8d77\u6765\uff0c\u901f\u5ea6\u4e0a\u5dee\u4e9b<\/p>\n<p>\u4e09\uff0e\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>1\uff0epublic virtual int Add(objectvalue);<\/p>\n<p>\u5c06\u5bf9\u8c61\u6dfb\u52a0\u5230ArrayList\u7684\u7ed3\u5c3e\u5904<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\n\u5185\u5bb9\u4e3aabcde<\/p>\n<p>2\uff0epublic virtual void Insert(intindex,objectvalue);<\/p>\n<p>\u5c06\u5143\u7d20\u63d2\u5165ArrayList\u7684\u6307\u5b9a\u7d22\u5f15\u5904<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\naList.Insert(0,&#8221;aa&#8221;);<\/p>\n<p>\u7ed3\u679c\u4e3aaaabcde<\/p>\n<p>3\uff0epublic virtual void InsertRange(intindex,ICollectionc);<\/p>\n<p>\u5c06\u96c6\u5408\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u63d2\u5165ArrayList\u7684\u6307\u5b9a\u7d22\u5f15\u5904<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\nArrayListlist2=newArrayList();<br \/>\nlist2.Add(&#8220;tt&#8221;);<br \/>\nlist2.Add(&#8220;ttt&#8221;);<br \/>\naList.InsertRange(2,list2);<\/p>\n<p>\u7ed3\u679c\u4e3aabtttttcde<\/p>\n<p>\u56db\uff0e\u5220\u9664<\/p>\n<p>a)public virtual void Remove(objectobj);<\/p>\n<p>\u4eceArrayList\u4e2d\u79fb\u9664\u7279\u5b9a\u5bf9\u8c61\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879,\u6ce8\u610f\u662f\u7b2c\u4e00\u4e2a<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\naList.Remove(&#8220;a&#8221;);<\/p>\n<p>\u7ed3\u679c\u4e3abcde<\/p>\n<p>2.public virtual void RemoveAt(intindex);<\/p>\n<p>\u79fb\u9664ArrayList\u7684\u6307\u5b9a\u7d22\u5f15\u5904\u7684\u5143\u7d20<\/p>\n<p>aList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\naList.RemoveAt(0);<\/p>\n<p>\u7ed3\u679c\u4e3abcde<\/p>\n<p>3\uff0epublic virtual void RemoveRange(intindex,intcount);<\/p>\n<p>\u4eceArrayList\u4e2d\u79fb\u9664\u4e00\u5b9a\u8303\u56f4\u7684\u5143\u7d20\u3002Index\u8868\u793a\u7d22\u5f15\uff0ccount\u8868\u793a\u4ece\u7d22\u5f15\u5904\u5f00\u59cb\u7684\u6570\u76ee<\/p>\n<p>aList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\naList.RemoveRange(1,3);<\/p>\n<p>\u7ed3\u679c\u4e3aae<\/p>\n<p>4\uff0epublic virtual void Clear();<\/p>\n<p>\u4eceArrayList\u4e2d\u79fb\u9664\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p>\u4e94\uff0e\u6392\u5e8f<\/p>\n<p>a)public virtual void Sort();<\/p>\n<p>\u5bf9ArrayList\u6216\u5b83\u7684\u4e00\u90e8\u5206\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\nDropDownList1.DataSource=aList;\/\/DropDownListDropDownList1;<br \/>\nDropDownList1.DataBind();<\/p>\n<p>\u7ed3\u679c\u4e3aeabcd<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\naList.Sort();\/\/\u6392\u5e8f<br \/>\nDropDownList1.DataSource=aList;\/\/DropDownListDropDownList1;<br \/>\nDropDownList1.DataBind();<\/p>\n<p>\u7ed3\u679c\u4e3aabcde<\/p>\n<p>b)public virtual void Reverse();<\/p>\n<p>\u5c06ArrayList\u6216\u5b83\u7684\u4e00\u90e8\u5206\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\u53cd\u8f6c\u3002<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\naList.Reverse();\/\/\u53cd\u8f6c<br \/>\nDropDownList1.DataSource=aList;\/\/DropDownListDropDownList1;<br \/>\nDropDownList1.DataBind();<br \/>\n\u7ed3\u679c\u4e3aedcba<\/p>\n<p>\u516d\uff0e\u67e5\u627e<\/p>\n<p>a)public virtual int IndexOf(object);<br \/>\nb)public virtual int IndexOf(object,int);<br \/>\nc)public virtual int IndexOf(object,int,int);<\/p>\n<p>\u8fd4\u56deArrayList\u6216\u5b83\u7684\u4e00\u90e8\u5206\u4e2d\u67d0\u4e2a\u503c\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4ece\u96f6\u5f00\u59cb\u7684\u7d22\u5f15\u3002\u6ca1\u627e\u5230\u8fd4\u56de-1\u3002<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\nintnIndex=aList.IndexOf(\u201ca\u201d);\/\/1<br \/>\nnIndex=aList.IndexOf(\u201cp\u201d);\/\/\u6ca1\u627e\u5230\uff0c-1<br \/>\nd)publicvirtualintLastIndexOf(object);<br \/>\ne)publicvirtualintLastIndexOf(object,int);<br \/>\nf)publicvirtualintLastIndexOf(object,int,int);<\/p>\n<p>\u8fd4\u56deArrayList\u6216\u5b83\u7684\u4e00\u90e8\u5206\u4e2d\u67d0\u4e2a\u503c\u7684\u6700\u540e\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4ece\u96f6\u5f00\u59cb\u7684\u7d22\u5f15\u3002<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;a&#8221;);\/\/\u540c0<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);<br \/>\nintnIndex=aList.LastIndexOf(&#8220;a&#8221;);\/\/\u503c\u4e3a2\u800c\u4e0d\u662f0<\/p>\n<p>g)public virtual bool Contains(objectitem);<\/p>\n<p>\u786e\u5b9a\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5728ArrayList\u4e2d\u3002\u5305\u542b\u8fd4\u56detrue,\u5426\u5219\u8fd4\u56defalse<\/p>\n<p>\u4e03\uff0e\u5176\u4ed6<\/p>\n<p>1\uff0epublic virtual int Capacity{get;set;}<\/p>\n<p>\u83b7\u53d6\u6216\u8bbe\u7f6eArrayList\u53ef\u5305\u542b\u7684\u5143\u7d20\u6570\u3002<\/p>\n<p>2\uff0epublic virtual int Count{get;}<\/p>\n<p>\u83b7\u53d6ArrayList\u4e2d\u5b9e\u9645\u5305\u542b\u7684\u5143\u7d20\u6570\u3002<br \/>\nCapacity\u662fArrayList\u53ef\u4ee5\u5b58\u50a8\u7684\u5143\u7d20\u6570\u3002Count\u662fArrayList\u4e2d\u5b9e\u9645\u5305\u542b\u7684\u5143\u7d20\u6570\u3002Capacity\u603b\u662f\u5927\u4e8e\u6216\u7b49\u4e8eCount\u3002\u5982\u679c\u5728\u6dfb\u52a0\u5143\u7d20\u65f6\uff0cCount\u8d85\u8fc7Capacity\uff0c\u5219\u8be5\u5217\u8868\u7684\u5bb9\u91cf\u4f1a\u901a<\/p>\n<p>\u8fc7\u81ea\u52a8\u91cd\u65b0\u5206\u914d\u5185\u90e8\u6570\u7ec4\u52a0\u500d\u3002<br \/>\n\u5982\u679cCapacity\u7684\u503c\u663e\u5f0f\u8bbe\u7f6e\uff0c\u5219\u5185\u90e8\u6570\u7ec4\u4e5f\u9700\u8981\u91cd\u65b0\u5206\u914d\u4ee5\u5bb9\u7eb3\u6307\u5b9a\u7684\u5bb9\u91cf\u3002\u5982\u679cCapacity\u88ab\u663e\u5f0f\u8bbe\u7f6e\u4e3a0\uff0c\u5219\u516c\u5171\u8bed\u8a00\u8fd0\u884c\u5e93\u5c06\u5176\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u5bb9\u91cf\u3002\u9ed8\u8ba4\u5bb9\u91cf\u4e3a16\u3002<br \/>\n\u5728\u8c03\u7528Clear\u540e\uff0cCount\u4e3a0\uff0c\u800c\u6b64\u65f6Capacity\u5207\u662f\u9ed8\u8ba4\u5bb9\u91cf16\uff0c\u800c\u4e0d\u662f0<\/p>\n<p>3\uff0epublic virtual void TrimToSize();<\/p>\n<p>\u5c06\u5bb9\u91cf\u8bbe\u7f6e\u4e3aArrayList\u4e2d\u5143\u7d20\u7684\u5b9e\u9645\u6570\u91cf\u3002<br \/>\n\u5982\u679c\u4e0d\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\uff0c\u5219\u6b64\u65b9\u6cd5\u53ef\u7528\u4e8e\u6700\u5c0f\u5316\u5217\u8868\u7684\u5185\u5b58\u7cfb\u7edf\u5f00\u9500\u3002<br \/>\n\u82e5\u8981\u5b8c\u5168\u6e05\u9664\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u8bf7\u5728\u8c03\u7528TrimToSize\u4e4b\u524d\u8c03\u7528Clear\u65b9\u6cd5\u3002\u622a\u53bb\u7a7aArrayList\u4f1a\u5c06ArrayList\u7684\u5bb9\u91cf\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u5bb9\u91cf\uff0c\u800c\u4e0d\u662f\u96f6\u3002<\/p>\n<p>ArrayListaList=newArrayList();<br \/>\naList.Add(&#8220;a&#8221;);<br \/>\naList.Add(&#8220;b&#8221;);<br \/>\naList.Add(&#8220;c&#8221;);<br \/>\naList.Add(&#8220;d&#8221;);<br \/>\naList.Add(&#8220;e&#8221;);\/\/Count=5,Capacity=16,<br \/>\naList.TrimToSize();\/\/Count=Capacity=5;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>System.Collections.ArrayList\u7c7b\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u6570\u7ec4\u3002\u901a\u8fc7\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u5c31\u53ef\u4ee5\u52a8\u6001\u6539\u53d8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-697","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":4593,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions\/4593"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}