通过下面表格进行对比:
异同点 | 普通集合 ArrayList和Hashtable | 泛型集合List<T>与Dictionary<K,V> |
不同点
| ArrayList的添加元素的Add方法返回int类型,代表添加成功后该元素在集合中的索引位置; | 泛型集合List<T>的添加元素的Add方法返回类型为void |
Hashtable集合遍历可以使用 foreach (DictionaryEntry de in employees) { MessageBox.Show("元素的key为为:"+de.Key.ToString()+",元素的信息为:"+((SE)de.Value).Show()); }
| Dictionary<K,V>的遍历只有下面二种: MessageBox.Show("--Foreach的keys 遍历信息--"); foreach (string o in employees.Keys) { SE s = employees[o.ToString()]; MessageBox.Show(s.Show()); }
MessageBox.Show("--Foreach的values 遍历信息--"); foreach (SE s in employees.Values) { MessageBox.Show(s.Show()); } | |
可以增加任何类型,属于弱类型操作 | 增加元素时类型严格检查,属于强类型操作 | |
添加和读取元素时,需要做装箱和拆箱操作 | 添加和读取元素时,无需做装箱和拆箱操作 | |
相同点 | 除了上面的区别外,ArrayList与List<T>、Hashtable与Dictionary<K,V>的操作基本相同 |
之所以泛型集合在编程被频繁使用(基本上替代了数组和普通集合),主要是其有以下优点:
1、实现了代码重用,未来的主流技术;
2、性能高,避免了繁琐的装箱和拆箱操作;
3、提供了更好的类型安全性(添加元素时做类型检查)。
4、CLR(公共语言运行时)支持泛型。
泛型集合List<T>的操作相关代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class FormList : Form { List<SE> employees; SE s; public FormList() { InitializeComponent(); } private void btnAdd_Click(object sender, EventArgs e) { SE se = new SE(); se.No = txtNo.Text; se.Name = txtName.Text; se.Age = Convert.ToInt32(txtAge.Text); employees.Add(se); s = se; MessageBox.Show("添加成功"); } private void Form1_Load(object sender, EventArgs e) { employees = new List<SE> { new SE { No = "s1000", Name = "张三", Age = 20 }, new SE { No = "s1001", Name = "李四", Age = 21 }, new SE{No="s1002",Name="王五",Age=22}}; } private void btnCount_Click(object sender, EventArgs e) { MessageBox.Show("List集合中元素的个数为:"+employees.Count); } private void btnShow_Click(object sender, EventArgs e) { int index = -1; bool bSuccess = int.TryParse(txtIndex.Text.Trim(), out index); if (bSuccess) { if (index > employees.Count -1) { MessageBox.Show("输入的索引下标超出范围"); } else { SE s = employees[index]; MessageBox.Show(s.Show()); } } else { MessageBox.Show("请输入整数类型!"); } } private void btnForeach_Click(object sender, EventArgs e) { for (int i = 0; i < employees.Count; i++) { MessageBox.Show(employees[i].Show()); } MessageBox.Show("------Foreach的遍历信息-----------"); foreach (SE s in employees) { MessageBox.Show(s.Show()); } } private void btnDelete_Click(object sender, EventArgs e) { MessageBox.Show("删除前的集合中的所有素为:"); for (int i = 0; i < employees.Count; i++) { MessageBox.Show(employees[i].Show()); } employees.RemoveAt(1); MessageBox.Show("删除索引下标为1的元素后的集合中的元素为:"); for (int i = 0; i < employees.Count; i++) { MessageBox.Show(employees[i].Show()); } employees.Remove(s); MessageBox.Show("删除最后添加的元素后的集合中的元素为:"); for (int i = 0; i < employees.Count; i++) { MessageBox.Show(employees[i].Show()); } employees.Clear(); MessageBox.Show("清空集合后的集合中的元素为:"); for (int i = 0; i < employees.Count; i++) { MessageBox.Show(employees[i].Show()); } } private void btnAddPM_Click(object sender, EventArgs e) { PM pm = new PM(); pm.No = 1001; pm.Name = "张三"; pm.YearOfWork = 5; //employees.Add(pm); MessageBox.Show("项目经理信息添加失败!"); } } }
泛型集合Dictionary<K,V>的操作相关代码如下:
using System; using System.Collections; using System.Collections.Generic; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; nam using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; nam using System; using System.Collections; using System.Collections.Generic; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; nam using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class FormDictionary : Form { Dictionary<string,SE> employees; public FormDictionary() { InitializeComponent(); } private void btnAdd_Click(object sender, EventArgs e) { SE se = new SE(); se.No = txtNo.Text; se.Name = txtName.Text; se.Age = Convert.ToInt32(txtAge.Text); employees.Add(se.No, se); MessageBox.Show("添加成功!"); } private void Form1_Load(object sender, EventArgs e) { employees = new Dictionary<string, SE>(); SE s1 = new SE(); s1.No = "s0001"; s1.Name = "张三"; s1.Age = 19; SE s2 = new SE(); s2.No = "s0002"; s2.Name = "李四"; s2.Age = 22; SE s3 = new SE(); s3.No = "s0003"; s3.Name = "王五"; s3.Age = 20; employees.Add(s1.No, s1); employees.Add(s2.No, s2); employees.Add(s3.No, s3); } private void btnCount_Click(object sender, EventArgs e) { MessageBox.Show("Dictionary集合中元素的个数为:"+employees.Count); } private void btnShow_Click(object sender, EventArgs e) { string key = txtSeNo.Text.Trim(); if (employees.ContainsKey(key)) { SE se = employees[key]; MessageBox.Show(se.Show()); } else { MessageBox.Show(key+"不在字典中!"); } } private void btnForeach_Click(object sender, EventArgs e) { MessageBox.Show("------Foreach的keys 遍历信息-----------"); foreach (string o in employees.Keys) { SE s = employees[o.ToString()]; MessageBox.Show(s.Show()); } MessageBox.Show("------Foreach的values 遍历信息-----------"); foreach (SE s in employees.Values) { MessageBox.Show(s.Show()); } } private void btnDelete_Click(object sender, EventArgs e) { employees.Remove("s0002"); MessageBox.Show("删除员工的编号为's0002'后的集合中的元素为:"); foreach (SE s in employees.Values) { MessageBox.Show(s.Show()); } employees.Clear(); MessageBox.Show("清空集合后的集合中的元素为:"); foreach (SE s in employees.Values) { MessageBox.Show(s.Show()); } } } }