/// <summary>
/// 它代表了可被单独索引的对象的有序集合。
/// 它基本上可以替代一个数组。但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,
/// 动态数组会自动重新调整它的大小。它也允许在列表中进行动态内存分配、增加、搜索、排序各项。
/// </summary>
public void ArrayList_Test()
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add(4);
// now: 1 2 4
list.Insert(2,3);
// now: 1 2 3 4
list.RemoveAt(3);
// now: 1 2 3
list.Insert(1, 5);
list.Sort();
// now: 1 2 3 5
}
/// <summary>
/// Hashtable 类代表了一系列基于键的哈希代码组织起来的键/值对。它使用键来访问集合中的元素。
/// 当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。
/// 哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。
/// </summary>
public void Hashtable_Test()
{
Hashtable ht = new Hashtable();
ht.Add(1, "1");
ht.Add(2, "2");
ht.Add(3, "1");
ht[3] = "3";
if(ht.ContainsKey(2))
{
// do somethoiing;
}
}
/// <summary>
/// 它可以使用键和索引来访问列表中的项。
/// 排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。
/// 如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,
/// 则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。
/// </summary>
public void SortedList_Test()
{
SortedList slist = new SortedList();
slist.Add(1, "1");
slist.Add(2, "2");
slist.Add(3, "3");
slist[1] = "11";
Console.WriteLine("0:"+ slist.GetByIndex(0));
}
/// <summary>
/// 堆栈(Stack)代表了一个后进先出的对象集合。
/// 当您需要对各项进行后进先出的访问时,则使用堆栈。
/// 当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。
/// </summary>
public void Stack_Test()
{
Stack s = new Stack();
s.Push(1);
s.Push(2);
s.Push(3);
// now;3 2 1
//返回并 移除顶端元素
s.Pop();
// now;2 1
s.Pop();
// now;1
}
/// <summary>
/// 它代表了一个先进先出的对象集合。
/// 当您需要对各项进行先进先出的访问时,则使用队列。
/// 当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队
/// </summary>
public void Queue_Test()
{
Queue q = new Queue();
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
// now:1 2 3
q.Dequeue();
// now:2 3
q.Dequeue();
// now:3
}
/// <summary>
/// 它代表了一个使用值 1 和 0 来表示的二进制数组。
/// 当您需要存储位,但是事先不知道位数时,则使用点阵列。
/// 您可以使用整型索引从点阵列集合中访问各项,索引从零开始。
/// </summary>
public void BitArray_Test()
{
// 在使用时:true代表1,false代表0;
BitArray array = new BitArray(5, false);
// array.Length = 5;
array.Set(0, true);
array.Set(1, true);
array.Set(2, true);
//now: 1 1 1 0 0
array.Not();
//now: 0 0 0 1 1
}
引用: