C#中的Queue.Contains()方法
C#中的Queue.Contains()方法用于确定某个元素是否在队列中。
语法
语法如下 -
public virtual bool Contains (object ob);
上面的 ob 参数是要在队列中查找的对象。
示例
下面我们看一个示例 -
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); Console.WriteLine("Does the queue has element Jack? = "+queue.Contains("Jack")); queue.Clear(); Console.Write("Count of elements (updated) = "); Console.WriteLine(queue.Count); } }
输出
这将产生以下输出 -
Count of elements = 8 Does the queue has element Jack? = True Count of elements (updated) = 0
示例
下面我们看另一个示例 -
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); Console.Write("Count of elements = "); Console.WriteLine(queue.Count); Console.WriteLine("Does the queue has element 500? = "+queue.Contains(500)); queue.Clear(); Console.Write("Count of elements (updated) = "); Console.WriteLine(queue.Count); } }
输出
这将产生以下输出 -
Count of elements = 3 Does the queue has element 500? = False Count of elements (updated) = 0
广告