将一个对象添加到队列末端 - C# 中的入队操作
要将一个对象添加到队列末尾,代码如下 -
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Queue<string> queue = new Queue<string>();
queue.Enqueue("Electronics");
queue.Enqueue("Accessories");
queue.Enqueue("Toys");
queue.Enqueue("Books");
queue.Enqueue("Furniture");
queue.Enqueue("Clothing");
queue.Enqueue("Footwear");
queue.Enqueue("Cookware");
queue.Enqueue("Pet Supplies");
Console.WriteLine("Elements in the Queue...");
foreach(var element in queue){
Console.WriteLine(element);
}
Console.WriteLine("Do the queue has the element Books? = "+queue.Contains("Books"));
}
}结果
这将产生以下结果 -
Elements in the Queue... Electronics Accessories Toys Books Furniture Clothing Footwear Cookware Pet Supplies Do the queue has the element Books? = True
示例
现在让我们看另一个示例 -
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Queue<string> queue = new Queue<string>();
queue.Enqueue("Gary");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
queue.Enqueue("Mark");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
Console.Write("Count of elements = ");
Console.WriteLine(queue.Count);
queue.Clear();
Console.Write("Count of elements (updated) = ");
Console.WriteLine(queue.Count);
}
}结果
这将产生以下结果 -
Count of elements = 8 Count of elements (updated) = 0
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP