Queue.Clear 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从 Queue 中移除所有对象。
public:
virtual void Clear();
public virtual void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()
示例
以下示例演示如何清除 的值 Queue。
using namespace System;
using namespace System::Collections;
void PrintValues( Queue^ myQ );
int main()
{
// Creates and initializes a new Queue.
Queue^ myQ = gcnew Queue;
myQ->Enqueue( "The" );
myQ->Enqueue( "quick" );
myQ->Enqueue( "brown" );
myQ->Enqueue( "fox" );
myQ->Enqueue( "jumps" );
// Displays the count and values of the Queue.
Console::WriteLine( "Initially," );
Console::WriteLine( " Count : {0}", myQ->Count );
Console::Write( " Values:" );
PrintValues( myQ );
// Clears the Queue.
myQ->Clear();
// Displays the count and values of the Queue.
Console::WriteLine( "After Clear," );
Console::WriteLine( " Count : {0}", myQ->Count );
Console::Write( " Values:" );
PrintValues( myQ );
}
void PrintValues( Queue^ myQ )
{
IEnumerator^ myEnum = myQ->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ myObj = safe_cast<Object^>(myEnum->Current);
Console::Write( " {0}", myObj );
}
Console::WriteLine();
}
/*
This code produces the following output.
Initially,
Count : 5
Values: The quick brown fox jumps
After Clear,
Count : 0
Values:
*/
using System;
using System.Collections;
public class SamplesQueue {
public static void Main() {
// Creates and initializes a new Queue.
Queue myQ = new Queue();
myQ.Enqueue( "The" );
myQ.Enqueue( "quick" );
myQ.Enqueue( "brown" );
myQ.Enqueue( "fox" );
myQ.Enqueue( "jumps" );
// Displays the count and values of the Queue.
Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", myQ.Count );
Console.Write( " Values:" );
PrintValues( myQ );
// Clears the Queue.
myQ.Clear();
// Displays the count and values of the Queue.
Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", myQ.Count );
Console.Write( " Values:" );
PrintValues( myQ );
}
public static void PrintValues( Queue myQ ) {
foreach ( Object myObj in myQ ) {
Console.Write( " {0}", myObj );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Initially,
Count : 5
Values: The quick brown fox jumps
After Clear,
Count : 0
Values:
*/
Imports System.Collections
Public Class SamplesQueue
Public Shared Sub Main()
' Creates and initializes a new Queue.
Dim myQ As New Queue()
myQ.Enqueue("The")
myQ.Enqueue("quick")
myQ.Enqueue("brown")
myQ.Enqueue("fox")
myQ.Enqueue("jumps")
' Displays the count and values of the Queue.
Console.WriteLine("Initially,")
Console.WriteLine(" Count : {0}", myQ.Count)
Console.Write(" Values:")
PrintValues(myQ)
' Clears the Queue.
myQ.Clear()
' Displays the count and values of the Queue.
Console.WriteLine("After Clear,")
Console.WriteLine(" Count : {0}", myQ.Count)
Console.Write(" Values:")
PrintValues(myQ)
End Sub
Public Shared Sub PrintValues(myQ As Queue)
Dim myObj As [Object]
For Each myObj In myQ
Console.Write(" {0}", myObj)
Next myObj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' Initially,
' Count : 5
' Values: The quick brown fox jumps
' After Clear,
' Count : 0
' Values:
注解
Count 将设置为零,同时还会释放集合元素中其他对象的引用。
容量保持不变。 若要重置 的 Queue容量,请调用 TrimToSize。 剪裁空 Queue 会将 的 Queue 容量设置为默认容量。
此方法是一个 O(n)
操作,其中 n
是 Count。