專題圖: 編號:ylbtech DotNet100010013
1,ArrayList(數組列表) |
Implements the IList interface using an array whose size is dynamically increased as required.
【提供一些方法,用於創建、處理、搜索數組並對數組進行排序,從而充當公共語言運行時中所有數組的基類。】
命名空間: System.Collections
程序集: mscorlib(在 mscorlib.dll 中)
2,Syntax(語法) |
[SerializableAttribute] [ComVisibleAttribute(true)] public class ArrayList : IList, ICollection, IEnumerable, ICloneable
3,備注: |
ArrayList 可能並不總是提供特定任務的最佳性能。 有關這些類相對性能的討論參見。 List<T> “性能注意事項”部分引用主題。
ArrayList 不保證是排序的。 在執行需要對 ArrayList 排序的操作(如 BinarySearch)之前,必須對 ArrayList 進行排序。
ArrayList 的容量是 ArrayList 可以包含的元素數。 隨着向 ArrayList 中添加元素,容量通過重新分配按需自動增加。 可通過調用 TrimToSize 或通過顯式設置 Capacity 屬性減少容量。
對於非常大 ArrayList 對象,則在運行時環境 (ide) 中增加最大容量為 20億在 64 位系統的元素通過設置 gcAllowVeryLargeObjects 配置元素的 enabled 屬性設置為 true 。
可使用一個整數索引訪問此集合中的元素。 此集合中的索引從零開始。
ArrayList 集合接受 null 作為有效值並且允許重復的元素。
不支持將多維數組用作 ArrayList 集合中的元素。
引用: http://msdn.microsoft.com/zh-cn/library/system.collections.arraylist.aspx
4,ArrayList【示例】 |
using System; using System.Collections; namespace ConsoleApplication1 { class Program { /// <summary> /// ylb_menu:ArrayList(數組列表) /// </summary> /// <param name="args"></param> static void Main(string[] args) { ArrayList al = new ArrayList(5); //添加 al.Add("sunshine"); al.Add("rain"); al.Add("sun"); al.Add("rain"); //查找項 if (al.Contains("sunshine")) { Console.WriteLine("數組包含“sunshine”"); } //清空數組 //al.Clear(); Console.WriteLine("數組的個數" + al.Count); //查找根據值,得到元素索引位置 //IndexOf Console.WriteLine(al.IndexOf("yuanbo")); Console.WriteLine(al.IndexOf("rain")); Console.WriteLine(al.IndexOf("rain", 0)); Console.WriteLine(al.IndexOf("rain", 2, 2)); //LastIndexOf Console.WriteLine(al.LastIndexOf("rain")); //遍歷 Graph(al); //插入一個項 al.Insert(2, "snoopy"); Graph(al); //反轉 al.Reverse(); Graph(al); //排序 al.Sort(); Graph(al); //移除 al.Remove("sunshine"); //根據項 Graph(al); al.RemoveAt(2); //下標 Graph(al); ////把數組轉化為字符串 //Console.WriteLine(al.ToString()); //Array arr=al.ToArray(); //for (int i = 0;i< arr.Length; i++) //{ // Console.Write(arr.ToString()); //} string[] strs=new string[al.Count]; //付給字符串數組 al.CopyTo(strs); foreach(string s in strs) { Console.Write(s + "\t"); } } /// <summary> /// 遍歷數組 /// </summary> /// <param name="al"></param> static void Graph(ArrayList al) { foreach (string a in al) { Console.Write(a+"\t"); } Console.Write("\n"); } } }
![]() |
作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 |
最終目標 |
“代碼的國際化標准示例 ylb,tech”,最大程度地規范軟件編程開發統一,優質, 高效,易學,為建設軟件強國(中國)而努力。