C#中的Collections命名空间

System.Collections命名空间包含可使用的集合类和相关的接口。

该命名空间下的.NET非泛型集合类如下所示:

— System.Collections.ArrayList:数组集合类,使用大小可按动态增加的数组实现Ilist接口。
— System.Collections.BitArray:布尔集合类,管理位值的压缩数组,该值为布尔值。
— System.Collections.Queue:队列,表示对象的先进先出集合。
— System.Collections.Stack:堆栈,表示对象的简单的后进先出集合。
— System.Collections.Hashtable:哈希表,表示键/值对的集合,这些键/值对根据键的哈希代码进行组织
— System.Collections.SortedList:排序集合类,表示键/值对的集合,这些键和值按键排序并可按键和索引访问。

该命名空间下的.NET非泛型接口如下所示:

— System.Collections.ICollection:定义所有集合的大小,枚举器和同步方法
— System.Collections.IComparer:比较两个对象的方法
— System.Collections.IList:表示可按照索引单独访问一组对象
— System.Collections.IDictionary:表示键/值对的集合
— System.Collections.IDictionaryEnumerator:枚举字典的元素
— System.Collections.IEnumerator:支持在集合上进行简单迭代

转载于:https://www.cnblogs.com/fields/p/5627919.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# ,PriorityQueue 可以通过使用 System.Collections.Generic 命名空间下的 PriorityQueue 类来实现。需要注意的是,PriorityQueue 类并不是 C# 标准库的一部分,因此需要使用第三方库或者自己实现该类。 以下是一个使用 System.Collections.Generic 命名空间下的 PriorityQueue 类的示例代码: ```csharp using System; using System.Collections.Generic; public class PriorityQueue<T> where T : IComparable<T> { private List<T> data; public PriorityQueue() { this.data = new List<T>(); } public void Enqueue(T item) { data.Add(item); int ci = data.Count - 1; // child index; start at end while (ci > 0) { int pi = (ci - 1) / 2; // parent index if (data[ci].CompareTo(data[pi]) >= 0) break; // child item is larger than (or equal) parent so we're done T tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp; ci = pi; } } public T Dequeue() { // assumes pq is not empty; up to calling code int li = data.Count - 1; // last index (before removal) T frontItem = data[0]; // fetch the front data[0] = data[li]; data.RemoveAt(li); --li; // last index (after removal) int pi = 0; // parent index. start at front of pq while (true) { int ci = pi * 2 + 1; // left child index of parent if (ci > li) break; // no children so done int rc = ci + 1; // right child if (rc <= li && data[rc].CompareTo(data[ci]) < 0) ci = rc; // prefer smallest child if (data[pi].CompareTo(data[ci]) <= 0) break; // parent is smaller than (or equal to) smallest child so done T tmp = data[pi]; data[pi] = data[ci]; data[ci] = tmp; // swap parent and child pi = ci; } return frontItem; } public int Count { get { return data.Count; } } public override string ToString() { string s = ""; for (int i = 0; i < data.Count; ++i) s += data[i].ToString() + " "; s += "count = " + data.Count; return s; } public bool IsConsistent() { // is the heap property true for all data? if (data.Count == 0) return true; int li = data.Count - 1; // last index for (int pi = 0; pi < data.Count; ++pi) // each parent index { int lci = 2 * pi + 1; // left child index int rci = 2 * pi + 2; // right child index if (lci <= li && data[pi].CompareTo(data[lci]) > 0) return false; // if lc exists and it's greater than parent then bad. if (rci <= li && data[pi].CompareTo(data[rci]) > 0) return false; // check the right child too. } return true; // passed all checks } } ``` 以上代码实现了一个基本的 Priority Queue 类,可以用于存储任意实现了 IComparable<T> 接口的类型。在该类,Enqueue() 方法用于将元素加入队列,Dequeue() 方法用于取出队首元素并移除,Count 属性返回队列元素数量,IsConsistent() 方法用于检查队列是否符合堆的定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值