Queue<T> クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オブジェクトの先入れ先出しコレクションを表します。
generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollection
generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::ICollection
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
type Queue<'T> = class
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ICollection
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<'T>
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IEnumerable
Public Class Queue(Of T)
Implements ICollection, IEnumerable(Of T), IReadOnlyCollection(Of T)
Public Class Queue(Of T)
Implements ICollection, IEnumerable(Of T)
型パラメーター
- T
キュー内の要素の型を指定します。
- 継承
-
Queue<T>
- 属性
- 実装
例
次のコード例では、ジェネリック クラスのいくつかのメソッドを Queue<T> 示します。 このコード例では、既定の容量を持つ文字列のキューを作成し、 メソッドを Enqueue 使用して 5 つの文字列をキューに入れます。 キューの要素は列挙され、キューの状態は変更されません。 メソッドは Dequeue 、最初の文字列のデキューに使用されます。 メソッドは Peek キュー内の次の項目を確認するために使用され、メソッドを Dequeue 使用してキューをデキューします。
メソッドをToArray使用して配列を作成し、キュー要素をコピーした後、配列を受け取るIEnumerable<T>コンストラクターにQueue<T>渡され、キューのコピーが作成されます。 コピーの要素が表示されます。
キューのサイズの 2 倍の配列が作成され、 メソッドを CopyTo 使用して配列の中央から始まる配列要素がコピーされます。 コンストラクターは Queue<T> 、先頭に 3 つの null 要素を含むキューの 2 つ目のコピーを作成するために再び使用されます。
メソッドは Contains 、文字列 "4" がキューの最初のコピーにあることを示すために使用されます。その Clear 後、 メソッドはコピーをクリアし、 Count プロパティはキューが空であることを示します。
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}",
numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
Console.WriteLine("\nContents of the first copy:");
foreach( string number in queueCopy )
{
Console.WriteLine(number);
}
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue<string> queueCopy2 = new Queue<string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in queueCopy2 )
{
Console.WriteLine(number);
}
Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}",
queueCopy.Contains("four"));
Console.WriteLine("\nqueueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
}
}
/* This code example produces the following output:
one
two
three
four
five
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Contents of the first copy:
three
four
five
Contents of the second copy, with duplicates and nulls:
three
four
five
queueCopy.Contains("four") = True
queueCopy.Clear()
queueCopy.Count = 0
*/
open System
open System.Collections.Generic
let numbers = Queue()
numbers.Enqueue "one"
numbers.Enqueue "two"
numbers.Enqueue "three"
numbers.Enqueue "four"
numbers.Enqueue "five"
// A queue can be enumerated without disturbing its contents.
for number in numbers do
printfn $"{number}"
printfn $"\nDequeuing '{numbers.Dequeue()}'"
printfn $"Peek at next item to dequeue: {numbers.Peek()}"
printfn $"Dequeuing '{numbers.Dequeue()}'"
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
let queueCopy = numbers.ToArray() |> Queue
printfn $"\nContents of the first copy:"
for number in queueCopy do
printfn $"{number}"
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
let array2 = numbers.Count * 2 |> Array.zeroCreate
numbers.CopyTo(array2, numbers.Count)
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
let queueCopy2 = Queue array2
printfn $"\nContents of the second copy, with duplicates and nulls:"
for number in queueCopy2 do
printfn $"{number}"
printfn $"""\nqueueCopy.Contains "four" = {queueCopy.Contains "four"}"""
printfn $"\nqueueCopy.Clear()"
queueCopy.Clear()
printfn $"queueCopy.Count = {queueCopy.Count}"
// This code example produces the following output:
// one
// two
// three
// four
// five
//
// Dequeuing 'one'
// Peek at next item to dequeue: two
// Dequeuing 'two'
//
// Contents of the first copy:
// three
// four
// five
//
// Contents of the second copy, with duplicates and nulls:
//
//
//
// three
// four
// five
//
// queueCopy.Contains "four" = True
//
// queueCopy.Clear()
//
// queueCopy.Count = 0
Imports System.Collections.Generic
Module Example
Sub Main
Dim numbers As New Queue(Of String)
numbers.Enqueue("one")
numbers.Enqueue("two")
numbers.Enqueue("three")
numbers.Enqueue("four")
numbers.Enqueue("five")
' A queue can be enumerated without disturbing its contents.
For Each number As String In numbers
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "Dequeuing '{0}'", numbers.Dequeue())
Console.WriteLine("Peek at next item to dequeue: {0}", _
numbers.Peek())
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue())
' Create a copy of the queue, using the ToArray method and the
' constructor that accepts an IEnumerable(Of T).
Dim queueCopy As New Queue(Of String)(numbers.ToArray())
Console.WriteLine(vbLf & "Contents of the first copy:")
For Each number As String In queueCopy
Console.WriteLine(number)
Next
' Create an array twice the size of the queue, compensating
' for the fact that Visual Basic allocates an extra array
' element. Copy the elements of the queue, starting at the
' middle of the array.
Dim array2((numbers.Count * 2) - 1) As String
numbers.CopyTo(array2, numbers.Count)
' Create a second queue, using the constructor that accepts an
' IEnumerable(Of T).
Dim queueCopy2 As New Queue(Of String)(array2)
Console.WriteLine(vbLf & _
"Contents of the second copy, with duplicates and nulls:")
For Each number As String In queueCopy2
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "queueCopy.Contains(""four"") = {0}", _
queueCopy.Contains("four"))
Console.WriteLine(vbLf & "queueCopy.Clear()")
queueCopy.Clear()
Console.WriteLine(vbLf & "queueCopy.Count = {0}", _
queueCopy.Count)
End Sub
End Module
' This code example produces the following output:
'
'one
'two
'three
'four
'five
'
'Dequeuing 'one'
'Peek at next item to dequeue: two
'
'Dequeuing 'two'
'
'Contents of the copy:
'three
'four
'five
'
'Contents of the second copy, with duplicates and nulls:
'
'
'
'three
'four
'five
'
'queueCopy.Contains("four") = True
'
'queueCopy.Clear()
'
'queueCopy.Count = 0
注釈
このクラスは、汎用キューを循環配列として実装します。 に Queue<T> 格納されているオブジェクトは、一方の端に挿入され、もう一方の端から削除されます。 キューとスタックは、情報の一時ストレージが必要な場合に便利です。つまり、その値を取得した後に要素を破棄する場合です。 コレクションに格納されているのと同じ順序で情報にアクセスする必要がある場合は、 を使用 Queue<T> します。 情報に逆の順序でアクセスする必要がある場合は、 を使用 Stack<T> します。 複数のスレッドから同時にコレクションにアクセスする必要がある場合は、 または ConcurrentStack<T> を使用ConcurrentQueue<T>します。
とその要素に対して、次の 3 つのメイン操作をQueue<T>実行できます。
Queue<T>の容量は、Queue<T>が保持できる要素の数です。 要素が に Queue<T>追加されると、内部配列を再割り当てすることで、必要に応じて容量が自動的に増加します。 を呼び出 TrimExcessすことで、容量を減らすことができます。
Queue<T> は参照 null
型の有効な値として受け入れ、重複する要素を許可します。
コンストラクター
Queue<T>() |
空で、既定の初期量を備えた、Queue<T> クラスの新しいインスタンスを初期化します。 |
Queue<T>(IEnumerable<T>) |
指定したコレクションからコピーした要素を格納し、コピーされる要素の数を格納できるだけの容量を備えた、Queue<T> クラスの新しいインスタンスを初期化します。 |
Queue<T>(Int32) |
空で、指定した初期量を備えた、Queue<T> クラスの新しいインスタンスを初期化します。 |
プロパティ
Capacity |
オブジェクトの先入れ先出しコレクションを表します。 |
Count |
Queue<T> に格納されている要素の数を取得します。 |
メソッド
Clear() |
Queue<T> からすべてのオブジェクトを削除します。 |
Contains(T) |
ある要素が Queue<T> 内に存在するかどうかを判断します。 |
CopyTo(T[], Int32) |
Queue<T> の要素を既存の 1 次元の Array にコピーします。コピー操作は、配列内の指定したインデックスから始まります。 |
Dequeue() |
Queue<T> の先頭にあるオブジェクトを削除し、返します。 |
Enqueue(T) |
Queue<T> の末尾にオブジェクトを追加します。 |
EnsureCapacity(Int32) |
このキューの容量が、少なくとも指定された であることを確認します |
Equals(Object) |
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
GetEnumerator() |
Queue<T> を反復処理する列挙子を返します。 |
GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
MemberwiseClone() |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
Peek() |
Queue<T> の先頭にあるオブジェクトを削除せずに返します。 |
ToArray() |
Queue<T> の要素を新しい配列にコピーします。 |
ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
TrimExcess() |
Queue<T> 内にある実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します。 |
TrimExcess(Int32) |
オブジェクトの先入れ先出しコレクションを表します。 |
TryDequeue(T) |
Queue<T> の先頭にあるオブジェクトを削除し、それを |
TryPeek(T) |
Queue<T> の先頭にオブジェクトがあるかどうかを示す値を返します。存在する場合は、それを |
明示的なインターフェイスの実装
ICollection.CopyTo(Array, Int32) |
ICollection の要素を Array にコピーします。Array の特定のインデックスからコピーが開始されます。 |
ICollection.IsSynchronized |
ICollection へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。 |
ICollection.SyncRoot |
ICollection へのアクセスを同期するために使用できるオブジェクトを取得します。 |
IEnumerable.GetEnumerator() |
コレクションを反復処理する列挙子を返します。 |
IEnumerable<T>.GetEnumerator() |
コレクションを反復処理する列挙子を返します。 |
拡張メソッド
適用対象
スレッド セーフ
パブリック静的 (Visual Basic ではShared
) なこの型のメンバーはスレッド セーフです インスタンス メンバーの場合は、スレッド セーフであるとは限りません。
は Queue<T> 、コレクションが変更されていない限り、複数のリーダーを同時にサポートできます。 それでも、コレクションを列挙することは本質的にスレッド セーフなプロシージャではありません。 スレッド セーフなキューについては、「」を参照してください ConcurrentQueue<T>。
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示