ArrayList.GetEnumerator 方法

定義

傳回在 ArrayList 中逐一查看的列舉值。

多載

GetEnumerator()

傳回整個 ArrayList 的列舉值。

GetEnumerator(Int32, Int32)

傳回適用於 ArrayList 中某段範圍項目的列舉程式。

GetEnumerator()

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
ArrayList.cs

傳回整個 ArrayList 的列舉值。

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public virtual System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Overridable Function GetEnumerator () As IEnumerator

傳回

整個 ArrayListIEnumerator

實作

範例

下列範例會取得的列舉值 ArrayList,以及 中 ArrayList專案範圍的列舉值。

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        ArrayList colors = new ArrayList();
        colors.Add("red");
        colors.Add("blue");
        colors.Add("green");
        colors.Add("yellow");
        colors.Add("beige");
        colors.Add("brown");
        colors.Add("magenta");
        colors.Add("purple");

        IEnumerator e = colors.GetEnumerator();
        while (e.MoveNext())
        {
            Object obj = e.Current;
            Console.WriteLine(obj);
        }

        Console.WriteLine();

        IEnumerator e2 = colors.GetEnumerator(2, 4);
        while (e2.MoveNext())
        {
            Object obj = e2.Current;
            Console.WriteLine(obj);
        }
    }
}

/* This code example produces
   the following ouput:
    red
    blue
    green
    yellow
    beige
    brown
    magenta
    purple

    green
    yellow
    beige
    brown
 */
Imports System.Collections

Class Program
    Private Shared Sub Main(ByVal args As String())
        Dim colors As New ArrayList()
        colors.Add("red")
        colors.Add("blue")
        colors.Add("green")
        colors.Add("yellow")
        colors.Add("beige")
        colors.Add("brown")
        colors.Add("magenta")
        colors.Add("purple")
        
        Dim e As IEnumerator = colors.GetEnumerator()
        While e.MoveNext()
            Dim obj As [Object] = e.Current
            Console.WriteLine(obj)
        End While
        
        Console.WriteLine()
        
        Dim e2 As IEnumerator = colors.GetEnumerator(2, 4)
        While e2.MoveNext()
            Dim obj As [Object] = e2.Current
            Console.WriteLine(obj)
        End While
    End Sub
End Class

' This code example produces
' the following ouput:
' red
' blue
' green
' yellow
' beige
' brown
' magenta
' purple
'
' green
' yellow
' beige
' brown
'

備註

C# 語言的 foreach 陳述式 (在 Visual Basic 中為 for each) 會隱藏列舉值的複雜度。 因此,建議您使用 foreach,而不要直接使用列舉值。

列舉程式可以用來讀取集合中的資料,但是無法用來修改基礎集合。

一開始,列舉程式位在集合中的第一個項目之前。 Reset 也會將列舉值帶回至這個位置。 在這個位置上,Current 並未定義。 因此,在讀取 MoveNext 的值之前,必須呼叫 Current 以將列舉值前移至集合的第一個項目。

Current 會傳回相同的物件直到呼叫 MoveNextResetMoveNext 會將 Current 設定為下一個項目。

如果 MoveNext 傳遞集合結尾,列舉值會放在集合的最後一個專案後面,並 MoveNextfalse回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false。 如果最後一 MoveNext 次呼叫傳 false回 , Current 則為未定義。 若要再次將 Current 設定為集合的第一個元素,您可以在呼叫 Reset 之後,接著呼叫 MoveNext

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如加入、修改或刪除項目,列舉程式會永久失效,且其行為未定義。

列舉程式沒有集合的獨佔存取權,因此,列舉集合內容本質上並不是安全的執行緒程序。 若要確保列舉期間的執行緒安全性,您可以在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。

這個方法是作業 O(1)

另請參閱

適用於

GetEnumerator(Int32, Int32)

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
ArrayList.cs

傳回適用於 ArrayList 中某段範圍項目的列舉程式。

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator(int index, int count);
public virtual System.Collections.IEnumerator GetEnumerator (int index, int count);
abstract member GetEnumerator : int * int -> System.Collections.IEnumerator
override this.GetEnumerator : int * int -> System.Collections.IEnumerator
Public Overridable Function GetEnumerator (index As Integer, count As Integer) As IEnumerator

參數

index
Int32

列舉程式應參考之 ArrayList 區段以零為起始的起始索引。

count
Int32

列舉程式應參考之 ArrayList 區段中的項目數。

傳回

適用於 ArrayList 中某指定項目範圍的 IEnumerator

例外狀況

index 小於零。

-或-

count 小於零。

indexcount 未指定 ArrayList 中的有效範圍。

範例

下列範例會取得的列舉值 ArrayList,以及 中 ArrayList專案範圍的列舉值。

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        ArrayList colors = new ArrayList();
        colors.Add("red");
        colors.Add("blue");
        colors.Add("green");
        colors.Add("yellow");
        colors.Add("beige");
        colors.Add("brown");
        colors.Add("magenta");
        colors.Add("purple");

        IEnumerator e = colors.GetEnumerator();
        while (e.MoveNext())
        {
            Object obj = e.Current;
            Console.WriteLine(obj);
        }

        Console.WriteLine();

        IEnumerator e2 = colors.GetEnumerator(2, 4);
        while (e2.MoveNext())
        {
            Object obj = e2.Current;
            Console.WriteLine(obj);
        }
    }
}

/* This code example produces
   the following ouput:
    red
    blue
    green
    yellow
    beige
    brown
    magenta
    purple

    green
    yellow
    beige
    brown
 */
Imports System.Collections

Class Program
    Private Shared Sub Main(ByVal args As String())
        Dim colors As New ArrayList()
        colors.Add("red")
        colors.Add("blue")
        colors.Add("green")
        colors.Add("yellow")
        colors.Add("beige")
        colors.Add("brown")
        colors.Add("magenta")
        colors.Add("purple")
        
        Dim e As IEnumerator = colors.GetEnumerator()
        While e.MoveNext()
            Dim obj As [Object] = e.Current
            Console.WriteLine(obj)
        End While
        
        Console.WriteLine()
        
        Dim e2 As IEnumerator = colors.GetEnumerator(2, 4)
        While e2.MoveNext()
            Dim obj As [Object] = e2.Current
            Console.WriteLine(obj)
        End While
    End Sub
End Class

' This code example produces
' the following ouput:
' red
' blue
' green
' yellow
' beige
' brown
' magenta
' purple
'
' green
' yellow
' beige
' brown
'

備註

foreach Visual C++ 中 C# 語言 (for each 的語句, For Each Visual Basic) 會隱藏列舉值的複雜度。 因此,建議您使用 foreach,而不要直接使用列舉值。

列舉程式可以用來讀取集合中的資料,但是無法用來修改基礎集合。

一開始,列舉程式位在集合中的第一個項目之前。 Reset 也會將列舉值帶回至這個位置。 在這個位置上,Current 並未定義。 因此,在讀取 MoveNext 的值之前,必須呼叫 Current 以將列舉值前移至集合的第一個項目。

Current 會傳回相同的物件直到呼叫 MoveNextResetMoveNext 會將 Current 設定為下一個項目。

如果 MoveNext 傳遞集合結尾,列舉值會放在集合的最後一個專案後面,並 MoveNextfalse回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false。 如果最後一 MoveNext 次呼叫傳 false回 , Current 則為未定義。 若要再次將 Current 設定為集合的第一個元素,您可以在呼叫 Reset 之後,接著呼叫 MoveNext

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如加入、修改或刪除項目,列舉程式會永久失效,且其行為未定義。

列舉程式沒有集合的獨佔存取權,因此,列舉集合內容本質上並不是安全的執行緒程序。 若要確保列舉期間的執行緒安全性,您可以在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。

這個方法是作業 O(1)

另請參閱

適用於