C# 队列(Queue)

news/2025/2/13 23:52:31/

目录

一、概述

二、基本的用法

1.添加元素

2.取出元素

1)Dequeue 方法

2)Peek 方法

3.判断元素是否存在

4.获取队列的长度

5.遍历队列

6.清空容器

7.Queue 泛型类

三、结束


一、概述

表示对象的先进先出集合。

队列和其他的数据结构一样,是一种存储容器,它遵循先进先出的原则,能够存储任意类型,但并不能获取到指定的位置,只能存入和取出,取出元素后,Queue 内部的元素自动删除,其实 队列 和 栈 使用方法大致差不多,只是取出数据的顺序不一样。

官方文档参考:Queue 类 (System.Collections) | Microsoft Learn

二、基本的用法

1.添加元素

添加元素使用 Enqueue 方法

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue queue = new Queue();queue.Enqueue(1);queue.Enqueue("你好");queue.Enqueue(true);queue.Enqueue(6.5);Console.ReadKey();}}
}

2.取出元素

1)Dequeue 方法

取出元素后,元素会自动从 Queue 中删除

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue queue = new Queue();queue.Enqueue(1);queue.Enqueue("你好");queue.Enqueue(true);queue.Enqueue(6.5);var value = queue.Dequeue();Console.WriteLine("value:" + value);Console.WriteLine("长度:" + queue.Count);Console.ReadKey();}}
}

运行

从执行的结果可以看到,最先存储的,最先被取出去。 

2)Peek 方法

读取最先添加的一个元素,读取后,元素不会从 Queue 中删除,但是只能读取一个元素

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue queue = new Queue();queue.Enqueue(1);queue.Enqueue("你好");queue.Enqueue(true);queue.Enqueue(6.5);var value = queue.Peek();Console.WriteLine("value:" + value);Console.WriteLine("长度:" + queue.Count);Console.ReadKey();}}
}

运行

 

3.判断元素是否存在

使用 Contains 来判断元素是否存在

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue queue = new Queue();queue.Enqueue(1);queue.Enqueue("你好");queue.Enqueue(true);queue.Enqueue(6.5);if(queue.Contains(1))Console.WriteLine("存在");elseConsole.WriteLine("不存在");Console.ReadKey();}}
}

运行

 

4.获取队列的长度

长度的获取和 List 一样,使用 Count 属性

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue queue = new Queue();queue.Enqueue(1);queue.Enqueue("你好");queue.Enqueue(true);queue.Enqueue(6.5);Console.WriteLine("长度:" + queue.Count);Console.ReadKey();}}
}

运行

 

5.遍历队列

可以使用 foreach 遍历队列,遍历并不会移除元素

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue queue = new Queue();queue.Enqueue(1);queue.Enqueue("你好");queue.Enqueue(true);queue.Enqueue(6.5);foreach (var item in queue){Console.WriteLine(item);}Console.WriteLine("长度:" + queue.Count);Console.ReadKey();}}
}

运行

 

6.清空容器

使用 Clear 方法来清空队列

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue queue = new Queue();queue.Enqueue(1);queue.Enqueue("你好");queue.Enqueue(true);queue.Enqueue(6.5);Console.WriteLine("长度:" + queue.Count);queue.Clear();Console.WriteLine("长度:" + queue.Count);Console.ReadKey();}}
}

运行

 

7.Queue 泛型类

queue 和 stack 一样,都有一个泛型类,作用也一样,就是用来存储固定类型的数据,在使用时,需要加上指定的类型,在使用这些数据时,就没必要对每个数据都去类型检测,使用起来也更加规范。

using System;
using System.Collections;
using System.Collections.Generic;namespace Queue_Test
{internal class Program{static void Main(string[] args){Queue<string> queue = new Queue<string>();//向队列中添加元素queue.Enqueue("老一");queue.Enqueue("老二");queue.Enqueue("老三");//获取队列的数量int count = queue.Count;//队列中是否包含指定的 valuebool b = queue.Contains("老王");//Peek方法是返回顶部的对象而不将其从堆栈中移除string names = queue.Peek();//获取队列中的元素//每次调用 Dequeue 方法,获取并移除队列中队首的元素string s1 = queue.Dequeue();Console.WriteLine(s1);string s2 = queue.Dequeue();Console.WriteLine(s2);string s3 = queue.Dequeue();Console.WriteLine(s3);//清空队列queue.Clear();Console.ReadKey();}}
}

三、结束

queue 和 stack  的特征和使用方法大致相同,主要特点:先进先出,存在装箱拆箱,存储任意类型,无法获取指定位置元素,只能取出最先存储的元素。

end


http://www.ppmy.cn/news/90553.html

相关文章

听劝,不要什么都不懂就自学网络安全【黑客】

一、网络安全学习的误区 1.不要试图以编程为基础去学习网络安全 不要以编程为基础再开始学习网络安全&#xff0c;一般来说&#xff0c;学习编程不但学习周期长&#xff0c;且过渡到网络安全用到编程的用到的编程的关键点不多。一般人如果想要把编程学好再开始学习网络安全往…

百度历年笔试面试150题

百度历年笔试面试150题 1、用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回。 2、用C语言实现函数void * memmove(void *dest, const void *src, size_t n)。memmove 函数的功能是拷贝src所指的内存内容前n个字节到dest所指的地址上。 分析:由于可以…

jvm之对象大小分析

写在前面 本文看下计算对象大小相关内容。 1&#xff1a;基础内容 1.1&#xff1a;对象的结构 一个对象由对象头和对象体组成&#xff0c;其中对象头包含如下内容&#xff1a; 标记字&#xff08;mark word&#xff09;&#xff1a;存放GC年龄信息&#xff0c;对象锁信息等…

八、Spring Cloud Alibaba-seata分布式事务

一、引言 1、事务简介 事务(Transaction)是访问并可能更新数据中各种数据项的一个程序执行单元(unit&#xff09;。在关系数据库中&#xff0c;一个事务由一组SQL语向组成。事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。 原子性(atomic…

数据库提权

数据库提权的前提就是得到数据库的账号密码。在webshell或本地进行提权。 数据库提权分为四步&#xff1a; 1.服务探针&#xff0c;探测出数据库的类型&#xff08;端口扫描等&#xff09; 2.信息搜集&#xff0c;就是获取到数据库的账号密码。权限要高。 读取数据库密码的…

基于ROS2的costmap中Obstacle Layer中对障碍物信息的增加与删除机制的方案调研。

文章目录 1.背景2.目标3. 障碍物信息添加方式发送数据的数据结构与接收数据的数据结构 4. 障碍物清理机制4.1 参数过滤障碍物4.2 时间过滤障碍物4.3 优化光追算法过滤障碍物4.4 障碍物跟踪过滤障碍物4.4.1 无语义动态障碍物跟踪4.4.2 语义障碍物分层4.4.3 障碍物实例分割与类别…

使用CNN-LSTM来预测锂离子电池健康状态SOH(附代码)

对于电动汽车而言&#xff0c;动力锂电池的健康状态(State of Health,SOH)估算方法是电池管理系统中非常重要的一个方面。准确估计锂电池老化状态并预测电池剩余寿命对于电动汽车稳定安全运行有着重要的意义。借助数据驱动方法的思想&#xff0c;本文对锂离子电池寿命历史数据进…

C++ Primer第五版_第十八章习题答案(11~20)

文章目录 练习18.11练习18.12练习18.13练习18.14练习18.15练习18.16练习18.17练习18.18练习18.19练习18.20 练习18.11 为什么 what 函数不应该抛出异常&#xff1f; what中如果抛出异常&#xff0c;需要try catch捕获&#xff0c;再调用what&#xff0c;一直循环&#xff0c;直…