ArrayList, Hashtable, SortedList 和Stack都位于System.Collections空间下:
ArrayList不固定大小的对象数组,按照索引访问。
Hashtable是键/值队的集合,依据键的哈希代码进行组织,按照键来访问。
SortedList 存入的元素会自动按照键进行排序,可以按照所以或者键来访问;
Stack先进后出的对象集合。
exam:
ArrayList:有Add(),Remove(),GetValues()等方法
遍历操作:for(int i=0;i<list.Count;i++)
{
Console.WriteLine("第{0}个元素{1}",i+1,list[i]);
}
Hashtable:
遍历操作:foreach(DictionaryEntry de in list)
{
Console.WriteLine("键{0}----值{1}",de.Key,de.Value);
}
SortedList:
遍历操作:for(int i=0;i<list.Count;i++)
{
Console.WriteLine("键{0}----值{1}",list.GetKey(i),list.GetByIndex(i));
}
或者
foreach(DictionaryEntry de in list)
{
Console.WriteLine("键{0}>>>>值{1}",de.Key,de.Value);
}
Stack:Push(),Pop()方法
遍历操作:foreach(object o in list)
{
Console.WriteLine("元素{0}",o);
}
.net2.0框架引入了泛型的概念,提供各种泛型集合代替普通集合,简单对应;
| 普通集合 | 泛型集合 |
| ArrayList | List<T> |
| Stack | Stack<T> |
| Queue | Queue<T> |
| DictionaryEntry | KeyValuePair<T> |
| Hashtable | Dictionary<T> |
| Comparer | Comparer<T> |
本文介绍了.NET框架中几种常用集合类,包括ArrayList、Hashtable、SortedList和Stack的基本特性和使用方法,并给出了每种集合类的遍历示例。
124

被折叠的 条评论
为什么被折叠?



