学用 ASP.Net 之 System.Collections.ArrayList 类
ArrayList 是 .Net 的动态数组.
主要成员:
/* 静态方法 */ ArrayList.Adapter() //把其他 IList 对象包装为 ArrayList 使用 ArrayList.FixedSize() //包装为固定容量 ArrayList.ReadOnly() //包装为只读 ArrayList.Repeat() //根据指定次数的重复值创建 ArrayList /* 属性 */ Capacity //容量 Count //元素数 IsFixedSize //是否被包装为固定容量? IsReadOnly //是否被包装为只读? /* 方法 */ Add() //添加 AddRange() //添加一个集合 BinarySearch() //使用二分法快速查找, 查找前须先排序; 找不到会返回一个 <0 的整数 Clear() //清除 Contains() //是否包含指定元素 CopyTo() //复制到指定数组, 要求类型兼容 GetRange() //截取 IndexOf() //获取指定元素的首次出现的索引 Insert() //插入 InsertRange() //插入一个集合 LastIndexOf() //获取指定元素的最后出现的索引 Remove() //移除指定元素 RemoveAt() //移除指定索引的元素 RemoveRange() //移除指定范围的元素 Reverse() //翻转, 可指定要翻转的范围 SetRange() //把指定集合复制过来 Sort() //排序, 可指定要排序范围和排序器 ToArray() //复制到新数组, 可同时进行类型转换 TrimToSize() //优化容量(Capacity = Count)
元素值可以是不同类型、可重复、可为 null:
protected void Button1_Click(object sender, EventArgs e) { ArrayList arr = new ArrayList(); arr.Add("ABC"); arr.Add(123); arr.Add(3.14); arr.Add(null); arr.Add(null); string s = arr[0].ToString(); int n = (int)arr[1]; double f = (double)arr[2]; TextBox1.Text = string.Concat(s, ", ", n, ", ", f); //ABC, 123, 3.14 //TextBox1.Text = string.Concat(arr[0], ", ", arr[1], ", ", arr[2]); //同上 }
ArrayList.Repeat()、AddRange() 及遍历:
protected void Button1_Click(object sender, EventArgs e) { ArrayList arr1 = ArrayList.Repeat("AAA", 3); ArrayList arr2 = ArrayList.Repeat("BBB", 4); arr1.AddRange(arr2); string s1, s2; s1 = s2 = ""; foreach (var x in arr1) { s1 += x.ToString() + " "; //AAA AAA AAA BBB BBB BBB BBB } for (int i = 0; i < arr1.Count; i++) { s2 += arr1[i].ToString() + " "; //AAA AAA AAA BBB BBB BBB BBB } TextBox1.Text = s1 + "\n" + s2; }
GetRange()、SetRange():
protected void Button1_Click(object sender, EventArgs e) { ArrayList arr1 = new ArrayList(6); //指定容量 arr1.Add(1); arr1.Add(2); arr1.Add(3); arr1.Add(4); arr1.Add(5); arr1.Add(6); ArrayList arr2 = arr1.GetRange(2, 3); string s1 = string.Join(", ", arr2.ToArray()); //3, 4, 5 arr1.SetRange(3, arr2); //arr2 中的元素将覆盖到 arr1 的指定位置, arr1 中须有位置才行 string s2 = string.Join(", ", arr1.ToArray()); //1, 2, 3, 3, 4, 5 TextBox1.Text = s1 + "\n" + s2; }
翻转与排序:
protected void Button1_Click(object sender, EventArgs e) { ArrayList arr = new ArrayList(10); //指定容量 arr.Add(1); arr.Add(3); arr.Add(5); arr.Add(7); arr.Add(9); arr.Add(2); arr.Add(4); arr.Add(6); arr.Add(8); arr.Add(0); arr.Reverse(); string s1 = string.Join(", ", arr.ToArray()); //0, 8, 6, 4, 2, 9, 7, 5, 3, 1 arr.Reverse(0, arr.Count / 2); string s2 = string.Join(", ", arr.ToArray()); //2, 4, 6, 8, 0, 9, 7, 5, 3, 1 arr.Sort(); string s3 = string.Join(", ", arr.ToArray()); //0, 1, 2, 3, 4, 5, 6, 7, 8, 9 TextBox1.Text = s1 + "\n" + s2 + "\n" + s3; }
插入与删除:
protected void Button1_Click(object sender, EventArgs e) { ArrayList arr = new ArrayList(9); //指定容量 for (int i = 0; i < arr.Capacity; ) { arr.Add(++i); } string s1 = string.Join(", ", arr.ToArray()); //1, 2, 3, 4, 5, 6, 7, 8, 9 arr.Insert(0, "A"); string s2 = string.Join(", ", arr.ToArray()); //A, 1, 2, 3, 4, 5, 6, 7, 8, 9 arr.Remove("A"); //有重复元素时, 删前面的 string s3 = string.Join(", ", arr.ToArray()); //1, 2, 3, 4, 5, 6, 7, 8, 9 arr.RemoveAt(arr.Count -1); string s4 = string.Join(", ", arr.ToArray()); //1, 2, 3, 4, 5, 6, 7, 8 arr.RemoveRange(4, 3); string s5 = string.Join(", ", arr.ToArray()); //1, 2, 3, 4, 8 arr.InsertRange(3, ArrayList.Repeat("A", 3)); string s6 = string.Join(", ", arr.ToArray()); //1, 2, 3, A, A, A, 4, 8 TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5 + "\n" + s6; }
IndexOf()、LastIndexOf():
protected void Button1_Click(object sender, EventArgs e) { char[] charArr = "google".ToCharArray(); ArrayList arr = new ArrayList(charArr); //其建立参数可以是一个 ICollection string s1 = string.Join(", ", arr.ToArray()); //g, o, o, g, l, e int n1 = arr.IndexOf('g'); //0 int n2 = arr.LastIndexOf('g'); //3 TextBox1.Text = string.Concat(s1, "\n", n1, "\n", n2); }
BinarySearch():
protected void Button1_Click(object sender, EventArgs e) { string[] strArr = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" }; ArrayList arr = new ArrayList(strArr); arr.Sort(); int i = arr.BinarySearch("nine"); //3 string str = string.Join(", ", arr.ToArray()); //eight, five, four, nine, one, seven, six, ten, three, two TextBox1.Text = string.Concat(i, "\n", str); }
FixedSize()、ReadOnly()、IsFixedSize、IsReadOnly:
protected void Button1_Click(object sender, EventArgs e) { ArrayList arr = new ArrayList(); arr.Add("one"); arr = ArrayList.FixedSize(arr); try { arr.Add("two"); } catch (Exception err) { Response.Write(err.Message); } //集合的大小是固定的。 finally { TextBox1.Text = "FixedSize = " + arr.IsFixedSize.ToString(); } //FixedSize = True } protected void Button2_Click(object sender, EventArgs e) { ArrayList arr = ArrayList.Repeat("000", 3); arr[0] = "AAA"; bool b1 = arr.IsReadOnly; //False arr = ArrayList.ReadOnly(arr); bool b2 = arr.IsReadOnly; //True //arr[0] = "BBB"; //再赋值会异常 TextBox1.Text = string.Concat(b1, "\n", b2); }