C#中Queue<T>类的使用以及部分方法的源代码分析
Queue<T>类
表示对象的先进先出集合。
队列在按接收顺序存储消息方面很实用,以便于进行顺序处理。 存储在 Queue,<T> 中的对象在一端插入,从还有一端移除。
Queue<T> 的容量是 Queue<T> 能够包括的元素数。 当向 Queue<T> 中加入元素时,将通过又一次分配内部数组来依据须要自己主动增大容量。
可通过调用 TrimExcess 来降低容量。
Queue<T> 接受 null 作为引用类型的有效值而且同意有反复的元素。
命名控件:System.Collections.Generic
程序集:System(在System.dll中)
语法:public class Queue<T>:IEnumerable<T>, ICollection, IEnumerable
List<T>实现了IList<T>、 ICollection<T>、IEnumerable<T>、IList、ICollection、IEnumerable接口
因此能够看出与List<T>相比:
Queue<T>没有继承ICollection<T>接口,由于这个接口定义的Add()和Remove()方法不能用于队列;
Queue<T>没有继承IList<T>接口,所以不能使用索引器訪问队列。
所以队列仅仅同意在队列的尾部加入元素。从队列的头部获取元素。
经常使用的Queue<T>类的成员:
Count : 返回队列中元素的个数。
Enqueue : 在队列一端加入一个元素。
Dequeue() : 从队列的头部读取和删除一个元素。
假设调用Dequeue()时,队列中没有元素就会抛出异常InvalidOperationException异常。
Peek(): 在队列的头部取出一个元素,但不删除它。
TrimExcess():重置队列的容量。
/******************************************************************************************************************************/
经常使用Queue<T>类的成员函数的源代码例如以下:
public T Dequeue()
{
if (this._size == 0)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EmptyQueue);
}
T local = this._array[this._head];
this._array[this._head] = default(T);
this._head = (this._head + 1) % this._array.Length;
this._size--;
this._version++;
return local;
}
public void Enqueue(T item)
{
if (this._size == this._array.Length)
{
int capacity = (int) ((this._array.Length * 200L) / 100L);
if (capacity < (this._array.Length + 4))
{
capacity = this._array.Length + 4;
}
this.SetCapacity(capacity);
}
this._array[this._tail] = item;
this._tail = (this._tail + 1) % this._array.Length;
this._size++;
this._version++;
}
public T Peek()
{
if (this._size == 0)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EmptyQueue);
}
return this._array[this._head];
}
public void TrimExcess()
{
int num = (int) (this._array.Length * 0.9);
if (this._size < num)
{
this.SetCapacity(this._size);
}
}
上诉方法使用到的SetCapacity函数的源代码例如以下:
private void SetCapacity(int capacity)
{
T[] destinationArray = new T[capacity];
if (this._size > 0)
{
if (this._head < this._tail)
{
Array.Copy(this._array, this._head, destinationArray, 0, this._size);
}
else
{
Array.Copy(this._array, this._head, destinationArray, 0, this._array.Length - this._head);
Array.Copy(this._array, 0, destinationArray, this._array.Length - this._head, this._tail);
}
}
this._array = destinationArray;
this._head = 0;
this._tail = (this._size == capacity) ? 0 : this._size;
this._version++;
}
/*****************************************************************************************************************************************/
演示样例:
以下的代码演示样例演示了 Queue<T> 泛型类的几个方法。
此代码演示样例创建一个具有默认容量的字符串队列,并使用 Enqueue 方法对五个字符串进行排队。 枚举队列元素,这不会更改队列的状态。 使用 Dequeue 方法使第一个字符串出列。 使用 Peek 方法查找队列中的下一项。然后使用 Dequeue 方法使该项出列。
使用 ToArray 方法创建一个数组并将队列元素拷贝到该数组,然后将该数组传递给接受 IEnumerable 的 Queue 构造函数以创建队列副本。 将显示副本的元素。
创建一个大小是队列大小两倍的数组,并使用 CopyTo 方法从数组中间開始复制数组元素。 再次使用 Queue1T> 构造函数创建第二个队列副本,此队列在開始处包括三个 null 元素。
使用 Contains 方法显示字符串“four”在第一个队列副本中。然后使用 Clear 方法清除此副本并由 Count 属性显示该队列为空。
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Queue1string> numbers = new Queue1string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}", numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable.
Queue1string> queueCopy = new Queue1string>(numbers.ToArray());
Console.WriteLine("\nContents of the first copy:");
foreach( string number in queueCopy )
{
Console.WriteLine(number);
}
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue1string> queueCopy2 = new Queue1string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in queueCopy2 )
{
Console.WriteLine(number);
}
Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}", queueCopy.Contains("four"));
Console.WriteLine("\nqueueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
}
}
/* This code example produces the following output:
one
two
three
four
five
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Contents of the copy:
three
four
five
Contents of the second copy, with duplicates and nulls:
three
four
five
queueCopy.Contains("four") = True
queueCopy.Clear()
queueCopy.Count = 0
*/
C#中Queue<T>类的使用以及部分方法的源代码分析的更多相关文章
- 已经mock类中引用的其它service类,但是在invoke私有方法的时候,该service类是空值
错误原因:没有在开始测试用例的时候,初始化类的所有注解方法. 解决方法: 使用mock方法创建mock对象时,需要在测试用例执行前执行以下代码.通常, 这句代码可以放在测试基类或者@Before 中. ...
- openstack中运行定时任务的两种方法及源代码分析
启动一个进程,如要想要这个进程的某个方法定时得进行执行的话,在openstack有两种方式: 一种是通过继承 periodic_task.PeriodicTasks,另一种是使用loopingcall ...
- Media Player Classic - HC 源代码分析 4:核心类 (CMainFrame)(3)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...
- Media Player Classic - HC 源代码分析 3:核心类 (CMainFrame)(2)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...
- Java中Queue类实现
原先在java编程中,Queue的实现都是用LinkedList Queue queue = new LinkedList(); 但正如jdk中所说的那样: 注意,此实现不是同步的.如果多个线程同时访 ...
- java中多线程中Runnable接口和Thread类介绍
java中的线程时通过调用操作系统底层的线程来实现线程的功能的. 先看如下代码,并写出输出结果. // 请问输出结果是什么? public static void main(String[] args ...
- JAVA中的集合容器操作类
目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...
- Java中是否可以调用一个类中的main方法?
前几天面试的时候,被问到在Java中是否可以调用一个类中的main方法?回来测试了下,答案是可以!代码如下: main1中调用main2的主方法 package org.fiu.test; impor ...
- 【实战Java高并发程序设计 1】Java中的指针:Unsafe类
是<实战Java高并发程序设计>第4章的几点. 如果你对技术有着不折不挠的追求,应该还会特别在意incrementAndGet() 方法中compareAndSet()的实现.现在,就让我 ...
随机推荐
- (转)浅谈测试驱动开发(TDD)
测试驱动开发(TDD)是极限编程的重要特点,它以不断的测试推动代码的开发,既简化了代码,又保证了软件质量.本文从开发人员使用的角度,介绍了 TDD 优势.原理.过程.原则.测试技术.Tips 等方面. ...
- c++ 快速幂 代码实现
懒得打代码系列… 不过这个代码挺短的死背下来也ok 解析在最下面 建议自己手动试个数据理解一下 比如 3^5 ^^ 原理:a ^ b = a ^ (b / 2) * 2 (b是奇数的话还要再乘一个a) ...
- JQuery中点击超链接动态修改url连接地址无效
这篇随笔的标题真是好拗口,想表达的意思是,当点击超链接后,才去修改超链接的地址,此时超链接仍然链接的是是修改之前的页面,而不是修改之后的页面. 超链接代码如下: <a id="chao ...
- Linux下安装MySQLdb模块(Python)
一.MySQLdb-python模块 https://pypi.python.org/pypi/MySQL-python ` 二.安装依赖包 yum -y install python-devel m ...
- luogu3231 [HNOI2013]消毒
前置技能:poj3041 如果是二维平面有一些方块,这些方块被染了黑色,你每次可以选择 \((x,y)\) 的区域染成白色,代价是 \(\min(x,y)\),问你付出的最小代价 显然我们不会这么染 ...
- API错误码设计-资料
搜索到一篇文章:新浪微博API错误代码说明对照表 可以参考新浪微博的错误码设计思路,设计自己系统的错误码.
- 02-offsetLeft和offsetTop
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- shell的case-esac
case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构. case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: ...
- hdu6069[素数筛法] 2017多校4
对于[l , r]内的每个数,根据唯一分解定理有 所以有 因为 //可根据唯一分解定理推导 所以 题目要求 就可以运用它到上述公式 (注意不能暴力对l,r内的数一个个分解算贡献 ...
- iOS学习笔记16-数据库SQLite
一.数据库 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等.离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式: 归档:NSKeyedArchiver 偏好设置:NSU ...