Array.GetEnumerator メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Array.GetEnumerator メソッドの意味・解説 

Array.GetEnumerator メソッド

Array の IEnumerator を返します

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

Public Function GetEnumerator As
 IEnumerator
Dim instance As Array
Dim returnValue As IEnumerator

returnValue = instance.GetEnumerator

戻り値
ArrayIEnumerator

解説解説

C# 言語foreach ステートメント (C++ の場合for eachVisual 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) 操作です。

使用例使用例

GetEnumerator使用して配列要素の一覧を表示する方法次のコード例示します

Imports System

Public Class SamplesArray

   Public Shared Sub Main()

      ' Creates and initializes a new Array.
      Dim myArr(10) As [String]
      myArr(0) = "The"
      myArr(1) = "quick"
      myArr(2) = "brown"
      myArr(3) = "fox"
      myArr(4) = "jumped"
      myArr(5) = "over"
      myArr(6) = "the"
      myArr(7) = "lazy"
      myArr(8) = "dog"

      ' Displays the values of the Array.
      Dim i As Integer =
 0
      Dim myEnumerator As System.Collections.IEnumerator
 = myArr.GetEnumerator()
      Console.WriteLine("The Array contains the following values:")
      While myEnumerator.MoveNext() And Not
 (myEnumerator.Current Is Nothing)
         Console.WriteLine("[{0}] {1}", i, myEnumerator.Current)
         i += 1
      End While 

   End Sub 'Main

End Class 'SamplesArray 


'This code produces the following output.
'
'The Array contains the following values:
'[0] The
'[1] quick
'[2] brown
'[3] fox
'[4] jumped
'[5] over
'[6] the
'[7] lazy
'[8] dog

using System;

public class SamplesArray  {
 
   public static void Main()
  {
 
      // Creates and initializes a new Array.
      String[] myArr = new String[10];
      myArr[0] = "The";
      myArr[1] = "quick";
      myArr[2] = "brown";
      myArr[3] = "fox";
      myArr[4] = "jumped";
      myArr[5] = "over";
      myArr[6] = "the";
      myArr[7] = "lazy";
      myArr[8] = "dog";
 
      // Displays the values of the Array.
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      Console.WriteLine( "The Array contains the following values:" );
      while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current
 != null ))
         Console.WriteLine( "[{0}] {1}", i++, myEnumerator.Current );

   }
 
}


/* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/

using namespace System;

int main()
{
   // Creates and initializes a new Array.
   array<String^>^myArr = gcnew array<String^>(10);
   myArr[ 0 ] = "The";
   myArr[ 1 ] = "quick";
   myArr[ 2 ] = "brown";
   myArr[ 3 ] = "fox";
   myArr[ 4 ] = "jumped";
   myArr[ 5 ] = "over";
   myArr[ 6 ] = "the";
   myArr[ 7 ] = "lazy";
   myArr[ 8 ] = "dog";
   
   // Displays the values of the Array.
   int i = 0;
   System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
   Console::WriteLine( "The Array contains the following values:" );
   while ( (myEnumerator->MoveNext()) && (myEnumerator->Current
 != nullptr) )
      Console::WriteLine( "[{0}] {1}", i++, myEnumerator->Current );
}

/* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/
import System.*;

public class SamplesArray
{
    public static void main(String[]
 args)
    {
        // Creates and initializes a new Array.
        String myArr[] = new String[10];

        myArr.set_Item(0, "The");
        myArr.set_Item(1, "quick");
        myArr.set_Item(2, "brown");
        myArr.set_Item(3, "fox");
        myArr.set_Item(4, "jumped");
        myArr.set_Item(5, "over");
        myArr.set_Item(6, "the");
        myArr.set_Item(7, "lazy");
        myArr.set_Item(8, "dog");

        // Displays the values of the Array.
        int i = 0;
        System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();

        Console.WriteLine("The Array contains the following values:");
        while ((myEnumerator.MoveNext() && myEnumerator.get_Current()
 != null)) {
            Console.WriteLine("[{0}] {1}", System.Convert.ToString(i++),
 
                myEnumerator.get_Current());
        }
    } //main
} //SamplesArray
 /* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumped
[5] over
[6] the
[7] lazy
[8] dog

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



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

辞書ショートカット

すべての辞書の索引

「Array.GetEnumerator メソッド」の関連用語

Array.GetEnumerator メソッドのお隣キーワード
検索ランキング

   

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



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

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

©2024 GRAS Group, Inc.RSS