{"id":1216,"date":"2010-07-23T09:53:00","date_gmt":"2010-07-23T01:53:00","guid":{"rendered":""},"modified":"2013-11-17T12:20:55","modified_gmt":"2013-11-17T04:20:55","slug":"c%e4%b8%adarray%e4%b8%8earraylist%e7%94%a8%e6%b3%95%e5%8f%8a%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1216.html","title":{"rendered":"C#\u4e2dArray\u4e0eArrayList\u7528\u6cd5\u53ca\u8f6c\u6362"},"content":{"rendered":"<p>\u4e00,C# array\u6570\u7ec4\u7684\u7528\u6cd5\u8303\u4f8b: <\/p>\n<p>type[]   typename=new type[size]; <\/p>\n<p>\u5982int[] a=new int[2];string[] str=new string[5];<\/p>\n<p>\u5b9e\u4e8b\u4e0a\u6211\u4eec\u5e73\u5e38\u662f\u7528int[],string[]&#8230;\u7684,\u6b64\u65f6\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e00\u4e2aArray\u6570\u7ec4,\u53ea\u4e0d\u8fc7\u6211\u4eec\u5e73\u5e38\u6ca1\u6709\u8fd9\u79cd\u610f\u8bc6\u800c\u5df2.<\/p>\n<p>(1):type\u6570\u636e\u7c7b\u578b\u4e0d\u80fd\u7f3a;\u4e14\u8981\u7edf\u4e00,\u800c\u4e0d\u80fd\u662f\u5982 int[] a=new Array[];<\/p>\n<p>(2):\u6570\u7ec4\u7684\u5927\u5c0fsize\u4e0d\u80fd\u7f3a,\u5426\u5219c#\u8ba4\u4e3a\u662f\u51fa\u9519\u7684,\u56e0\u4e3a\u6570\u7ec4\u662f\u4e00\u6bb5\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5b58;<\/p>\n<p>(3):\u53f3\u8fb9\u662f\u4e00\u4e2a\u4e2d\u62ec\u53f7[],\u800c\u4e0d\u662f()<\/p>\n<p>\u5b9e\u4f8b:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n            \/\/Array ar = new Array();\/\/\u9519\u8bef,\u65e0\u6cd5\u521b\u5efa\u62bd\u8c61\u7c7b\u6216\u63a5\u53e3\u201cSystem.Array\u201d\u7684\u5b9e\u4f8b\r\n            \/\/int&#x5B;] array = new Array&#x5B;3];\u9519\u8bef,\r\n            \/\/int&#x5B;] array = new Array&#x5B;];\u9519\u8bef,\u5fc5\u987b\u6307\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f\u6216\u521d\u59cb\u503c\r\n            \/\/int&#x5B;] array = new Array&#x5B;3];\/\/\u9519\u8bef,\u65e0\u6cd5\u8f6c\u6362\u4e3aint&#x5B;]\r\n            \/\/int&#x5B;] array=new int&#x5B;];\/\/\u9519\u8bef,\u5fc5\u987b\u6307\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f\u6216\u521d\u59cb\u503c\r\n            \/\/\u5e73\u5e38\u6211\u4eecint&#x5B;],string&#x5B;]...\u4e8b\u5b9e\u4e0a\u5c31\u662f\u58f0\u660e\u4e00\u4e2aarray\u6570\u7ec4\u4e86\r\n\r\n\r\n            int&#x5B;] array = new int&#x5B;5];\r\n            \/\/\u6216int&#x5B;] arr={1,2,3};\r\n            for (int i = 0; i &lt; 5; i++)\r\n            {\r\n                array&#x5B;i] = i*100;\/\/\u6ce8:array\u6570\u7ec4\u5e76\u4e0d\u63d0\u4f9badd,clear,addRange..\u65b9\u6cd5,\u800c\u662f\u76f4\u63a5\u8bbe\u7f6e\u6216\u83b7\u53d6\u503c\r\n                Response.Write(array&#x5B;i] + &quot;&lt;br&gt;&quot;);\r\n            }\r\n            Response.Write(array.Length + &quot;&lt;br&gt;&lt;br&gt;&quot;);\r\n<\/pre>\n<p>\u6216<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nint &#x5B;] intarray=new int&#x5B;9];\r\n     intarray&#x5B;0]=10;\r\n     intarray&#x5B;1]=20;\r\n     intarray&#x5B;2]=30;\r\n     intarray&#x5B;3]=40;\r\n     intarray&#x5B;4]=50;\r\n     intarray&#x5B;5]=60;\r\n     intarray&#x5B;6]=70;\r\n     intarray&#x5B;7]=80;\r\n     intarray&#x5B;8]=90;\r\n<\/pre>\n<p>\u4e8c,C# ArrayList\u6570\u7ec4\u7684\u7528\u6cd5\u8303\u4f8b:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nArrayList al = new ArrayList();\r\n            for (int i = 0; i &lt; 3; i++)\r\n            {\r\n                al.Add(i);\r\n                Response.Write(al&#x5B;i].ToString() + &quot;&lt;br&gt;&quot;);\/\/\u8f93\u51fa\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u503c\r\n                \/\/ Response.Write(al&#x5B;i]+ &quot;&lt;br&gt;&quot;);\r\n            }\r\n            Response.Write(al.Count + &quot;&lt;br&gt;&lt;br&gt;&quot;);\r\n\r\n            foreach (int obj in al)\/\/\u6216foreach (object obj in al),\u56e0\u4e3aal\u662f\u4e00\u4e2aobject\u7c7b\u578b\u7684\u6570\u7ec4\r\n            {\r\n                Response.Write(obj+&quot;-OK&quot;+&quot;&lt;br&gt;&quot;);\r\n            }\r\n<\/pre>\n<p>\u4e09,ArrayList\u548cArray\u76f8\u4e92\u4e4b\u95f4\u7684\u8f6c\u5316<\/p>\n<p>         (1)\u4ee5\u4e0b\u662f\u628aArrayList\u6570\u7ec4\u4e2d\u7684\u503c\u62f7\u8d1d\u5230Array\u4e2d\u53bb\u7684\u5b9e\u4f8b\u7528\u6cd5<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n          \/\/int&#x5B;] IResult=new int&#x5B;al.Count];\r\n            \/\/al.CopyTo(IResult);\r\n            \/\/\u6216\u662f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\r\n          int&#x5B;] IResult = (int&#x5B;])al.ToArray(typeof(Int32));\/\/ToArray(Int32);\u8fd9\u6837\u9519\u8bef,\u4e00\u5b9a\u8981\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\r\n          \/\/Person&#x5B;] personArrayFromList = (Person&#x5B;])personList.ToArray(typeof(Person));  \r\n\r\n         foreach(int item   in IResult)\r\n            {\r\n              Response.Write(item.ToString());\r\n            }\r\n\r\n          Response.Write(&quot;\u4ee5\u4e0b\u662f\u628aArray\u6570\u7ec4\u4e2d\u7684\u503c\u62f7\u8d1d\u5230ArrayList\u4e2d\u53bb\u7684\u5b9e\u4f8b\u7528\u6cd5&quot; + &quot;&lt;br&gt;&quot; + &quot;\u7ed3\u679c\u4e3a:&lt;br&gt;&quot;);\r\n\r\n            int&#x5B;] a ={ 222, 333, 555 };\r\n            ArrayList arrList = new ArrayList();\r\n            foreach (object obj in a)\/\/\u6216foreach (int obj in a)\r\n            {\r\n                arrList.Add(obj);\r\n                Response.Write(obj+&quot;&lt;br&gt;&quot;);\r\n            }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00,C# array\u6570\u7ec4\u7684\u7528\u6cd5\u8303\u4f8b: type[] typename=new type[size]; \u5982int [&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-1216","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1216","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=1216"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":4583,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1216\/revisions\/4583"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}