Queue.GetEnumeratorとは? わかりやすく解説

Queue.GetEnumerator メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Public Function GetEnumerator As
 Enumerator
Dim instance As Queue(Of
 T)
Dim returnValue As Enumerator

returnValue = instance.GetEnumerator

戻り値
Queue の Queue.Enumerator。

解説解説

C# 言語foreach ステートメント (C++ の場合for eachVisual Basic の場合For Each) を使用することで列挙子の複雑さ回避できます。したがって列挙子を直接操作するではなくforeach使用お勧めます。

列挙子を使用すると、コレクション内のデータ読み取ることができますが、基になるコレクション変更することはできません。

最初に列挙子はコレクション最初要素前に配置されます。この位置では、Current未定義です。したがってCurrent の値を読み取る前に、MoveNext を呼び出してコレクション最初要素列挙子を進める必要があります

Current は、MoveNext呼び出されるまでは同じオブジェクト返しますMoveNext は、Current次の要素設定します

MoveNextコレクション末尾を過ぎると、列挙子の位置コレクション最後要素後ろとなり、MoveNextfalse返します列挙子がこの位置にある場合以降MoveNext呼び出してfalse返されます。MoveNext への最後呼び出しfalse返され場合は、Current未定義です。Current を、再度コレクション最初要素設定することはできません。列挙子の新しインスタンス作成する必要があります

コレクション変更されない限り列挙子は有効なままです。要素追加変更削除などの変更コレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作未定義になります

列挙子はコレクションへの排他アクセス権持たないため、コレクション列挙処理は、本質的にスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションロックすることにより、列挙処理でのスレッド セーフ確保できますコレクション対し複数スレッドアクセスして読み取り書き込みを行うことができるようにするには、独自に同期化実装する必要があります

System.Collections.Generic のコレクション既定実装同期されません。

このメソッドは O(1) 操作です。

使用例使用例

Queue ジェネリック クラス列挙可能であることを示すコード例次に示しますforeach ステートメント (Visual Basic の場合For EachC++ の場合for each) を使用してキュー列挙します

このコード例では、既定容量文字列キュー作成しEnqueue メソッド使用して 5 つ文字列キュー置きますキュー要素列挙されますが、キューの状態は変化しません。Dequeue メソッド使用して最初文字列キューから取り出しますPeek メソッド使用してキュー内の次の項目を調べてから、Dequeue メソッド使用してキューから取り出します

ToArray メソッド使用して配列作成し、その配列キュー要素コピーしてから、IEnumerable を受け取Queue コンストラクタ配列渡してキューコピー作成しますコピー要素表示されます。

キューの 2 倍のサイズ配列作成されCopyTo メソッド使用して配列中央部で始まる配列要素コピーしますQueue コンストラクタ再度使用して先頭3 つの null 要素を含むキュー2 つ目のコピー作成します

Contains メソッドにより、文字列 "four" がキュー最初コピー含まれていることを示しますその後で、Clear メソッドコピークリアすると、Count プロパティによってキューが空であることが示されます。

Imports System
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
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 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.GetEnumerator メソッド

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

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Function
 GetEnumerator As IEnumerator
Dim instance As Queue
Dim returnValue As IEnumerator

returnValue = instance.GetEnumerator

戻り値
Queue の IEnumerator。

解説解説

C# 言語foreach ステートメント (Visual Basic の場合for each) を使用することで列挙子の複雑さ回避できます。したがって列挙子を直接操作するではなくforeach使用お勧めます。

列挙子を使用すると、コレクション内のデータ読み取ることができますが、基になるコレクション変更することはできません。

初期状態では、列挙子はコレクション最初要素前に位置してます。Reset実行した場合も、列挙子はこの位置に戻されます。この位置で、Current未定義です。したがってCurrent の値を読み取る前に、MoveNext を呼び出してコレクション最初要素列挙子を進める必要があります

Current は、MoveNext または Reset呼び出されるまでは同じオブジェクト返しますMoveNext は、Current次の要素設定します

MoveNextコレクション末尾を過ぎると、列挙子はコレクション最後要素後ろ配置されMoveNextfalse返します列挙子がこの位置にある場合以降MoveNext呼び出してfalse返されます。MoveNext への最後呼び出しfalse返され場合は、Current未定義です。Currentコレクション最初要素に再び設定するには、Reset呼び出してから、MoveNext呼び出します。

列挙子は、コレクション変更されない限り有効です。要素追加変更削除などの変更コレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作未定義になります

列挙子はコレクションへの排他アクセス権持たないため、コレクション列挙処理は、本質的にスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションロックすることにより、列挙処理でのスレッド セーフ確保できますコレクション対し複数スレッドアクセスして読み取り書き込みを行うことができるようにするには、独自に同期化実装する必要があります

このメソッドは O(1) 操作です。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からQueue.GetEnumeratorを検索した結果を表示しています。
Weblioに収録されているすべての辞書からQueue.GetEnumeratorを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からQueue.GetEnumerator を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Queue.GetEnumerator」の関連用語

Queue.GetEnumeratorのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Queue.GetEnumeratorのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS