次の方法で共有


Queue.Clear メソッド

Queue からすべてのオブジェクトを削除します。

Public Overridable Sub Clear()
[C#]
public virtual void Clear();
[C++]
public: virtual void Clear();
[JScript]
public function Clear();

解説

Count は 0 に設定されます。 Queue の容量をリセットするには、 TrimToSize を呼び出します。空の Queue に対して TrimToSize メソッドを使用すると、 Queue の容量は 0 ではなく、既定値に設定されます。

使用例

[Visual Basic, C#, C++] Queue の値を消去する方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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("jumped")
        
        ' 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(myCollection As IEnumerable)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Initially,
'    Count    : 5
'    Values:    The    quick    brown    fox    jumped
' After Clear,
'    Count    : 0
'    Values: 

[C#] 
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( "jumped" );

      // 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( IEnumerable myCollection )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "\t{0}", myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
   Count    : 5
   Values:    The    quick    brown    fox    jumped
After Clear,
   Count    : 0
   Values:
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myCollection )  {
   System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::Write( S"\t{0}", myEnumerator->Current );
   Console::WriteLine();
}

int main()  {

   // Creates and initializes a new Queue.
   Queue* myQ = new Queue();
   myQ->Enqueue( S"The" );
   myQ->Enqueue( S"quick" );
   myQ->Enqueue( S"brown" );
   myQ->Enqueue( S"fox" );
   myQ->Enqueue( S"jumped" );

   // Displays the count and values of the Queue.
   Console::WriteLine( S"Initially," );
   Console::WriteLine( S"   Count    : {0}", __box(myQ->Count) );
   Console::Write( S"   Values:" );
   PrintValues( myQ );

   // Clears the Queue.
   myQ->Clear();

   // Displays the count and values of the Queue.
   Console::WriteLine( S"After Clear," );
   Console::WriteLine( S"   Count    : {0}", __box(myQ->Count) );
   Console::Write( S"   Values:" );
   PrintValues( myQ );
}

 /*
 This code produces the following output.

 Initially,
    Count    : 5
    Values:    The    quick    brown    fox    jumped
 After Clear,
    Count    : 0
    Values:
 */

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Queue クラス | Queue メンバ | System.Collections 名前空間 | TrimToSize