检查元素是否在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<int> queue = new Queue<int>();
queue.Enqueue(100);
queue.Enqueue(200);
queue.Enqueue(300);
queue.Enqueue(400);
queue.Enqueue(500);
queue.Enqueue(600);
queue.Enqueue(700);
queue.Enqueue(800);
queue.Enqueue(1000);
Console.WriteLine("Elements in the Queue...");
foreach(var element in queue){
Console.WriteLine(element);
}
Console.WriteLine("Do the queue has the element 50? = "+queue.Contains(50));
}
}输出结果
这将产生以下输出-
Elements in the Queue... 100 200 300 400 500 600 700 800 1000 Do the queue has the element 50? = False
热门推荐
10 收到公公生日祝福语简短
11 广东新居入伙简短祝福语
12 生日祝福语简短蛋糕上
13 海鲜送礼祝福语简短大全
14 新年的好祝福语简短
15 送给姐姐简短的祝福语
16 新年祝同学祝福语简短
17 学生毕业季祝福语简短
18 收围巾的祝福语简短