此方法用于从队列中删除对象。此方法是O(n)运算,其中n是元素总数。而且此方法位于System.Collections命名空间下。
用法:
public void Clear ();
下面给出了一些示例,以更好地理解实现:
示例1:
// C# code to illustrate the
// Queue.Clear Method
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue
Queue q = new Queue();
// Inserting the elements into the Queue
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(2);
q.Enqueue(4);
// Displaying the count of elements
// contained in the Queue before
// removing all the elements
Console.Write("Total number of elements "+
"in the Queue are : ");
Console.WriteLine(q.Count);
// Removing all elements from Queue
q.Clear();
// Displaying the count of elements
// contained in the Queue after
// removing all the elements
Console.Write("Total number of elements"+
" in the Queue are : ");
Console.WriteLine(q.Count);
}
}
输出:
Total number of elements in the Queue are : 4 Total number of elements in the Queue are : 0
示例2:
// C# code to illustrate the
// Queue.Clear Method
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue
Queue q = new Queue();
// Inserting the elements into the Queue
q.Enqueue("C");
q.Enqueue("C++");
q.Enqueue("Java");
q.Enqueue("PHP");
q.Enqueue("HTML");
q.Enqueue("Python");
// Displaying the count of elements
// contained in the Queue before
// removing all the elements
Console.Write("Total number of elements "+
"in the Queue are : ");
Console.WriteLine(q.Count);
// Removing all elements from Queue
q.Clear();
// Displaying the count of elements
// contained in the Queue after
// removing all the elements
Console.Write("Total number of elements "+
"in the Queue are : ");
Console.WriteLine(q.Count);
}
}
输出:
Total number of elements in the Queue are : 6 Total number of elements in the Queue are : 0
参考:
相关用法
注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 Queue.Clear Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。