ArrayList 是非泛型動態陣列,**自動擴容**、**支援索引存取**、**可排序/反轉**,是早期 ASP.NET 儲存單一值序列的常用集合。本文示範 ArrayList 建立、排序操作、繫結清單控制項、與泛型 List<T> 比較。
System.CollectionsAdd(value) 尾端新增,Insert(index, value) 指定位置插入Sort() 升冪排序,Reverse() 反轉順序TrimToSize() 將容量調整為實際元素數量,釋放記憶體<%@ Import Namespace="System.Collections" %>
<script runat="server">
void Page_Load() {
if (!IsPostBack) {
ArrayList countries = new ArrayList();
countries.Add("挪威");
countries.Add("瑞典");
countries.Add("法國");
countries.Add("義大利");
// 優化容量
countries.TrimToSize();
// 排序(字母順序:法國、義大利、挪威、瑞典)
countries.Sort();
// 若需降冪:countries.Reverse();
rblCountries.DataSource = countries;
rblCountries.DataBind(); // ArrayList 無 Key/Value,直接以值為 Text 與 Value
}
}
</script>
<html><body>
<form runat="server">
<h3>國家選單(ArrayList 排序後):</h3>
<asp:RadioButtonList ID="rblCountries" runat="server" />
</form>
</body></html>
ArrayList list = new ArrayList();
list.Add("A"); // 索引 0
list.Add("B"); // 索引 1
list.Insert(1, "C"); // 索引 1 插入 "C",原 "B" 移至索引 2
// 結果:A, C, B
list.Remove("C"); // 移除第一個符合值
list.RemoveAt(0); // 移除指定索引
list.RemoveRange(1, 2); // 移除索引 1 起 2 個元素
int idx = list.IndexOf("B"); // 找值回傳索引(-1 表不存在)
bool exists = list.Contains("A");
ArrayList nums = new ArrayList { 3, 1, 4, 1, 5, 9 };
nums.Sort(); // 1, 1, 3, 4, 5, 9
nums.Reverse(); // 9, 5, 4, 3, 1, 1
// 自訂排序:實作 IComparer
nums.Sort(new CustomComparer());
ArrayList list = new ArrayList(100); // 指定初始容量 100
// ... 大量 Add ...
list.TrimToSize(); // 釋放多餘容量
int capacity = list.Capacity; // 目前配置容量
int count = list.Count; // 實際元素數
string[] arr = (string[])list.ToArray(typeof(string));
// 或 .NET 2.0+ 泛型擴充方法
string[] arr2 = list.Cast<string>().ToArray();
<asp:DropDownList ID="ddlCountries" runat="server"
DataSourceID="alCountries" />
繫結結果:每個 ListItem 的 Text 與 Value 皆為 ArrayList 元素值。
注意:若需要 Text 與 Value 不同(如顯示名稱、實際值為代碼),請改用
Hashtable、SortedList或Dictionary<string,string>。
| 特性 | ArrayList | List<T> (泛型) |
|---|---|---|
| 型別安全 | 否(object) | 是(編譯期檢查) |
| 效能:值型別 | 裝箱/拆箱開銷大 | 無裝箱,效能優 |
| 效能:參考型別 | 相近 | 略優(無型別轉換) |
| 語法便利性 | 集合初始化器 .NET 3.0+ | 完整支援、型別推論 |
| LINQ 支援 | 需 Cast<T>() 轉型 | 原生支援 |
| 序列化 | XmlSerializer 支援有限 | 完整支援 |
| 適用版本 | .NET 1.0+ | .NET 2.0+ |
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
void Page_Load() {
if (!IsPostBack) {
// 型別安全、語法簡潔
var countries = new List<string> { "挪威", "瑞典", "法國", "義大利" };
// LINQ 排序
var sorted = countries.OrderBy(c => c).ToList();
rblCountries.DataSource = sorted;
rblCountries.DataBind();
}
}
</script>
ArrayList/List<T> 皆可繫結:
RadioButtonList:單選群組CheckBoxList:多選群組DropDownList:下拉選單ListBox:清單方塊(SelectionMode="Multiple" 支援多選)BulletedList:項目符號清單DataList/Repeater/GridView:資料繫結控制項(需包裝為物件或匿名型別)若需 Text/Value 分離,建立簡單 DTO:
public class CountryItem {
public string Code { get; set; }
public string Name { get; set; }
}
// 後端
var items = new List<CountryItem> {
new CountryItem { Code = "TW", Name = "台灣" },
new CountryItem { Code = "JP", Name = "日本" }
};
ddlCountries.DataSource = items;
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "Code";
ddlCountries.DataBind();
Sort() 要求所有元素實作 IComparable,混合型別會拋 InvalidOperationExceptionAdd 會觸發重新配置陣列,僅在確定不再新增元素時呼叫ToString() 顯示型別名稱new ArrayList(10000)ArrayList 作為早期動態陣列實作已完成歷史使命,現代 ASP.NET 開發請全面採用 List<T> 享受型別安全、效能優勢與 LINQ 整合。下一節將總結資料繫結架構與支援繫結的控制項清單。