C#内置队列类Queue用法实例
作者:八大山人 发布时间:2023-07-18 14:12:05
标签:C#,队列
本文实例讲述了C#内置队列类Queue用法。分享给大家供大家参考。具体分析如下:
这里详细演示了C#内置的队列如何进行添加,移除等功能。
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
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<T>.
Queue<string> queueCopy = new Queue<string>(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).
Queue<string> queueCopy2 = new Queue<string>(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#程序设计有所帮助。


猜你喜欢
- 一、基本概念// 上下文对象 private Context context; public FileService(Context con
- 有三种方法如下:三个方法都需要动态申请读写权限否则保存图片到相册也会失败方法一/** * 保存bitmap到本地
- C#编写一个简易计算器,供大家参考,具体内容如下界面代码using System;using System.Collections.Gene
- 报错问题如图:仔细看报错问题后发现,这个错误的主要原因是:ValidatorException:PKIX path building fai
- 前面已经认识了不同的数据类型,你们有没有尝试过让不同的数据类型进行运算呢?int a = 1;double b = a;Console.Wr
- DSA数字签名,供大家参考,具体内容如下一、实验目的在掌握了ElGamal和Schorr数字签名算法的基础上,进一步地学习和掌握DSA签名算
- 工厂方法模式,往往是设计模式初学者入门的模式,的确,有人称之为最为典型最具启发效果的模式。android中用到了太多的工厂类,其中有用工厂方
- 数组翻转的方法(java实现),数组翻转,就是将数组倒置,例如原数组为:{"a","b","
- 概述一般我们在向文档添加水印时,会分为直接添加文字水印和加载图片添加图片水印两种情况。常见的,在添加文字水印时会多以声明文档版权、权威性的文
- 本文实例讲述了Java正则验证正整数的方法。分享给大家供大家参考,具体如下:package des;import java.util.reg
- 主要是应对这种需求:软件只允许启动一次。将这个问题转化一下,可以这样描述:对于一个软件,在启动一个进程之后,不允许启动其它进程,如果第二次打
- BeanUtilsBeanUtils是Apache commens组件里面的成员,由Apache提供的一套开源api,用于简化对javaBe
- 前言《黄金矿工》游戏是一个经典的抓金子小游戏,它可以锻炼人的反应能力。。该游戏中,可以通过“挖矿”获
- 目录带装饰效果的 ContainerRow 行布局和 Column列布局ListView列表组件GridView网格组件代码实现结语:左侧是
- Android ToggleButton 详解在Android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来
- 前言前段时间看到一道面试题:“main函数可以被重载么?”,当时就蒙圈了,怎么还会有这种面试题,现在
- Android开发过程中,特别是新开的项目,底部状态栏的切换使用的频率非常的高,主要的实现方式有:(1)、TabLayout + Fragm
- static关键字在Java中,static是静态修饰关键字。用于修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序性
- 在搭建Spring Cloud Eureka环境前先要了解整个架构的组成,常用的基础模式如下图:服务提供者:将springboot服务编写好
- 表单的重复提交: 没有完整的进行一次,先请求表单页面->再提交表单过程而完成数据提交造成的根本原因: 没有完整的进行一次,先请求表单页