HashTable Queue Stack SortedList BitArray

HashTable

由于是非泛型集合,因此存储进去的都是object类型,不管是还是

Hashtable不允许排序 key不允许重复不允许为null

Queue和Queue<T>

Queue成为队列,队列是这样一种数据结构,数据有列表的一端插入,并由列表的另一端移除。就像单行道,只能从一段进,从一端出。Queue类实现了ICollection和IEnumerable接口。


1、先进先出

  2、以添加null值到集合中

  3、许集合中的元素重复

  4、Queue容量会按需自动添加

  5、Queue的等比因子是当需要更大容量时当前容量要乘以的数字,默认是2.0。

Queue q = new Queue();
q.Enqueue(1);//添加记录
q.Enqueue("helloword!");
q.Enqueue(1.23);
Console.WriteLine(q.Peek()); //输出1 获取值但不移除,与出列不同 返回位于Queue开始出的对象,但不将其移除,与出列不同,出列是会移除的
int Count = q.Count; //出队的时候q.Count在变化,因此q.Count放在循环条件里是不妥的
for (int i = 0; i < Count; i++)
{
Console.WriteLine(q.Dequeue().ToString()); //注意 输出 1 想家了 1.23 都是从最先添加的先拿
}
Console.WriteLine(q.Count); //输出0 出列一次,就自动移除了。

Queue<int> qInt = new Queue<int>();
qInt.Enqueue(1);
qInt.Enqueue(2);
qInt.Enqueue(3);
Console.WriteLine(qInt.Peek()); //输出1
int IntCount = qInt.Count;
for (int i = 0; i < IntCount; i++)
{
Console.WriteLine(qInt.Dequeue()); //注意 输出 123 都是从最先添加的先拿
}
Console.WriteLine(q.Count); //输出0 出列一次,就自动移除了。

Stack

添加和移除都在一端进行 后进先出

1、后进先出。

  2、以添加null值到集合中。

  3、许集合中的元素重复

  4、Stack的容量会按需自动增加。

Stack s = new Stack();
s.Push(1);//添加
s.Push("helloword!");
s.Push(1.23);
Console.WriteLine(s.Peek()); //输出1.23 返回位于Stack顶部的对象,但不移除,注意出栈是移除的。它不移除仅仅返回。

int Count = s.Count; //差点犯了逻辑错误,在for里面如果是s.Count的话,很容易乱,因为出栈操作s.Count是在变动着的。
for (int i = 0; i < Count; i++)
{
Console.WriteLine(s.Pop()); //输出 1.23 想回家了 1 移除并返回Stack顶部的对象 出栈
}
Console.WriteLine(s.Count); //输出0


Stack<int> sInt = new Stack<int>();
sInt.Push(1);
sInt.Push(2);
sInt.Push(3);
Console.WriteLine(sInt.Peek()); //输出3

int IntCount = sInt.Count; //差点犯了逻辑错误,在for里面如果是s.Count的话,很容易乱,因为出栈操作s.Count是在变动着的。
for (int i = 0; i < IntCount; i++)
{
Console.WriteLine(sInt.Pop()); //输出 3 2 1
}
Console.WriteLine(sInt.Count); //输出0

SortedList与SortedList<T>

SortedList类实现了IDictionary、ICollection以及IEnumerable接口 键/值 键和索引 访问

键的排序

在SortedList中,键和值分别保存在一个数组中,当向Sorted添加一个元素时,SortedList类添加一个元素时,SortedList会首先对key进行排序,然后根据排序结果计算出要插入到集合中的位置索引,再分别将key和value插
入到各自数组的指定索引位置。当使用foreach循环集合中的元素时,SortedList会将相同索引位置的key和value放进一个DictionaryEntry类型的对象,然后返回。

SortedList SL = new SortedList();
SL.Add("txt","txt"); //Add的时候会自动排序
SL.Add("jpg","jpg");
SL.Add("png","png");
foreach (DictionaryEntry de in SL) //返回的是DictionaryEntry对象
{
Console.Write(de.Key + ":" + de.Value + " "); //输出 jpg:jpg png:png txt:txt //注意这个顺序啊,添加的时候就自动排序了
}

Console.WriteLine();
SortedList<int,string> SLString = new SortedList<int,string>();
SLString.Add(3, "333");
SLString.Add(2, "222");
SLString.Add(1, "111");
foreach (KeyValuePair<int,string> des in SLString) //返回的是KeyValuePair,在学习的时候尽量少用var,起码要知道返回的是什么
{
Console.Write(des.Key + ":" + des.Value + " ");
}

Console.ReadKey();

BitArray

位结构 二进制位(0和1)的集合
BitArray的值表示true或false。true表示位打开,false表示位关闭。BitArray实现了ICollection和IEnumerable接口。

1、BitArray集合不支持动态调整,因此没有Add和Remove方法。

  2、若需要调整集合的大小,可通过设置属性Length的值来实现。

  3、集合中的索引从0开始。

  4、使用BitArray(int length)构造函数初始化BitArray集合后,其值均为false

  5、BitArray集合之间支持按位“或”、“异或”、“与运算”,参与这三类运算的BitArray集合长度必须相等。否则会抛出异常。


BitArray BA = new BitArray(3);
BA[0] = true;
BA[1] = false;
BA[2] = true;

BitArray BB = new BitArray(3);
BA[0] = true;
BA[1] = false;
BA[2] = true;

BitArray BOr = BA.Or(BB); //与运算,返回一个新的BitArray
foreach (var b in BOr)
{
Console.Write(b + "-"); //True-False-True
}

原文地址:https://www.cnblogs.com/mmbbflyer/p/7699772.html