Queue クラス

定義

オブジェクトの先入れ先出しコレクションを表します。

public ref class Queue : System::Collections::ICollection
public ref class Queue : ICloneable, System::Collections::ICollection
public class Queue : System.Collections.ICollection
public class Queue : ICloneable, System.Collections.ICollection
[System.Serializable]
public class Queue : ICloneable, System.Collections.ICollection
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Queue : ICloneable, System.Collections.ICollection
type Queue = class
    interface ICollection
    interface IEnumerable
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
Public Class Queue
Implements ICollection
Public Class Queue
Implements ICloneable, ICollection
継承
Queue
属性
実装

次の例は、 に値を作成して追加する Queue 方法とその値を出力する方法を示しています。

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "Hello" );
   myQ->Enqueue( "World" );
   myQ->Enqueue( "!" );
   
   // Displays the properties and values of the Queue.
   Console::WriteLine( "myQ" );
   Console::WriteLine( "\tCount:    {0}", myQ->Count );
   Console::Write( "\tValues:" );
   PrintValues( myQ );
}

void PrintValues( IEnumerable^ myCollection )
{
   IEnumerator^ myEnum = myCollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 myQ
     Count:    3
     Values:    Hello    World    !
*/
 using System;
 using System.Collections;
 public class SamplesQueue  {

    public static void Main()  {

       // Creates and initializes a new Queue.
       Queue myQ = new Queue();
       myQ.Enqueue("Hello");
       myQ.Enqueue("World");
       myQ.Enqueue("!");

       // Displays the properties and values of the Queue.
       Console.WriteLine( "myQ" );
       Console.WriteLine( "\tCount:    {0}", myQ.Count );
       Console.Write( "\tValues:" );
       PrintValues( myQ );
    }

    public static void PrintValues( IEnumerable myCollection )  {
       foreach ( Object obj in myCollection )
          Console.Write( "    {0}", obj );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.

 myQ
     Count:    3
     Values:    Hello    World    !
*/
Imports System.Collections

Public Class SamplesQueue

    Public Shared Sub Main()

        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("Hello")
        myQ.Enqueue("World")
        myQ.Enqueue("!")

        ' Displays the properties and values of the Queue.
        Console.WriteLine("myQ")
        Console.WriteLine("    Count:    {0}", myQ.Count)
        Console.Write("    Values:")
        PrintValues(myQ)

    End Sub

    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' myQ
'     Count:    3
'     Values:    Hello    World    !

注釈

このクラスは、キューを循環配列として実装します。 に Queue 格納されているオブジェクトは、一方の端に挿入され、もう一方の端から削除されます。

重要

新しい開発には クラスを Queue 使用しないことをお勧めします。 代わりに、ジェネリック Queue<T> クラスを使用することをお勧めします。 詳細については、「GitHub で 非ジェネリック コレクションを使用しないでください 」を参照してください。

キューとスタックは、情報の一時ストレージが必要な場合に便利です。つまり、その値を取得した後に要素を破棄する場合です。 コレクションに格納されているのと同じ順序で情報にアクセスする必要がある場合は、 を使用 Queue します。 情報に逆の順序でアクセスする必要がある場合は、 を使用 Stack します。 複数のスレッドから同時にコレクションにアクセスする必要がある場合は、 または ConcurrentStack<T> を使用ConcurrentQueue<T>します。

とその要素に対して、次の 3 つのメイン操作をQueue実行できます。

  • Enqueue の末尾に 要素を追加します Queue

  • Dequeue は、 の先頭から最も古い要素を削除します Queue

  • Peek は、 の先頭 Queue にある最も古い要素を返しますが、 Queueから削除しません。

Queueの容量は、Queueが保持できる要素の数です。 要素が に Queue追加されると、再割り当てによって必要に応じて容量が自動的に増加します。 を呼び出 TrimToSizeすことで、容量を減らすことができます。

増加係数は、より大きな容量が必要な場合に現在の容量を乗算する数値です。 成長係数は、 が構築されるときに Queue 決定されます。 既定の増加率は 2.0 です。 の容量は、増加率に Queue 関係なく、常に少なくとも 4 つ増加します。 たとえば、増加率が 1.0 の の Queue 場合、より大きな容量が必要な場合、容量は常に 4 増加します。

Queuenull 有効な値として受け取り、重複する要素を許可します。

このコレクションの汎用バージョンについては、次を参照してください。 System.Collections.Generic.Queue<T>

コンストラクター

Queue()

空で、既定の初期量を備え、既定の増加率を使用する、Queue クラスの新しいインスタンスを初期化します。

Queue(ICollection)

指定したコレクションからコピーした要素を格納し、コピーした要素の数と同じ初期量を備え、既定の増加率を使用する、Queue クラスの新しいインスタンスを初期化します。

Queue(Int32)

空で、指定した初期量を備え、既定の増加率を使用する、Queue クラスの新しいインスタンスを初期化します。

Queue(Int32, Single)

空で、指定した初期量を備え、指定した増加率を使用する、Queue クラスの新しいインスタンスを初期化します。

プロパティ

Count

Queue に格納されている要素の数を取得します。

IsSynchronized

Queue へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

SyncRoot

Queue へのアクセスを同期するために使用できるオブジェクトを取得します。

メソッド

Clear()

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

Clone()

Queue の簡易コピーを作成します。

Contains(Object)

ある要素が Queue 内に存在するかどうかを判断します。

CopyTo(Array, Int32)

Queue の要素を既存の 1 次元の Array にコピーします。コピー操作は、配列内の指定したインデックスから始まります。

Dequeue()

Queue の先頭にあるオブジェクトを削除し、返します。

Enqueue(Object)

Queue の末尾にオブジェクトを追加します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

Queue を反復処理する列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Peek()

Queue の先頭にあるオブジェクトを削除せずに返します。

Synchronized(Queue)

元のキューをラップする、スレッド セーフである新しい Queue を返します。

ToArray()

Queue の要素を新しい配列にコピーします。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
TrimToSize()

容量を Queue 内にある実際の要素数に設定します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

スレッド セーフ

パブリック静的 (Visual Basic ではShared) なこの型のメンバーはスレッド セーフです インスタンス メンバーの場合は、スレッド セーフであるとは限りません。

のスレッド セーフを保証するには、 Queueメソッドによって返されるラッパーを使用して、すべての操作を実行する Synchronized(Queue) 必要があります。

コレクションの列挙処理は、本質的にスレッドセーフな処理ではありません。 コレクションが同期されていても、他のスレッドがコレクションを変更する場合があり、このときは列挙子から例外がスローされます。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

こちらもご覧ください