ArrayList.GetEnumerator 메서드

정의

ArrayList를 반복하는 열거자를 반환합니다.

오버로드

GetEnumerator()

전체 ArrayList에 대한 열거자를 반환합니다.

GetEnumerator(Int32, Int32)

ArrayList의 요소 범위에 대한 열거자를 반환합니다.

GetEnumerator()

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
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
'

설명

@FSHO1@C# 언어의 foreach 문(Visual Basic의 경우 for each)은 열거자의 복잡성을 숨깁니다. 그러므로 열거자를 직접 조작하는 대신 foreach를 사용하는 것이 좋습니다.

열거자를 사용하여 컬렉션의 데이터를 읽을 수는 있지만 내부 컬렉션을 수정할 수는 없습니다.

처음에 열거자는 컬렉션의 첫 번째 요소 앞에 배치됩니다. Reset을 실행하면 열거자가 이 위치로 복원됩니다. 이 위치에서 Current는 정의되지 않습니다. 따라서 MoveNext의 값을 읽기 전에 Current를 호출하여 열거자를 해당 컬렉션의 첫 번째 요소로 보내야 합니다.

Current에서는 MoveNext 또는 Reset이 호출될 때까지 동일한 개체를 반환합니다. MoveNextCurrent를 다음 요소로 설정합니다.

경우 MoveNext 열거자를 컬렉션의 끝 컬렉션의 마지막 요소 뒤에 배치 되는 전달 하 고 MoveNext 반환 false합니다. 열거자가 있는 경우이 위치에 대 한 후속 호출은 MoveNext 반환할 수도 false합니다. 반환 falseCurrent 된 에 대한 MoveNext 마지막 호출이 정의되지 않은 경우 Current를 컬렉션의 첫째 요소에 다시 설정하려면 Reset을 호출한 뒤 MoveNext를 호출해야 합니다.

컬렉션이 변경되지 않고 그대로 유지되는 한 열거자는 유효한 상태로 유지됩니다. 컬렉션에 대해 변경 내용을 수행하면(예: 요소 추가, 수정 또는 삭제) 열거자는 복구 불가능하게 무효화되고 해당 동작은 정의되지 않습니다.

열거자는 컬렉션에 배타적으로 액세스하지 못하므로 컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 열거 동안 스레드 보안을 보장하려면 전체 열거 동안 컬렉션을 잠그면 됩니다. 여러 스레드에서 컬렉션에 액세스하여 읽고 쓸 수 있도록 허용하려면 사용자 지정 동기화를 구현해야 합니다.

이 메서드는 작업입니다 O(1) .

추가 정보

적용 대상

GetEnumerator(Int32, Int32)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
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 섹션의 0부터 시작하는 인덱스입니다.

count
Int32

열거자가 참조해야 하는 ArrayList 섹션의 요소 수입니다.

반환

ArrayList의 지정된 요소 범위에 대한 IEnumerator입니다.

예외

index가 0보다 작은 경우

또는

count가 0보다 작은 경우

indexcountArrayList의 올바른 범위를 지정하지 않습니다.

예제

다음 예제에서는 의 에 대한 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# 언어(for eachVisual C++, For Each Visual Basic의 경우)의 문은 foreach 열거자의 복잡성을 숨깁니다. 그러므로 열거자를 직접 조작하는 대신 foreach를 사용하는 것이 좋습니다.

열거자를 사용하여 컬렉션의 데이터를 읽을 수는 있지만 내부 컬렉션을 수정할 수는 없습니다.

처음에 열거자는 컬렉션의 첫 번째 요소 앞에 배치됩니다. Reset을 실행하면 열거자가 이 위치로 복원됩니다. 이 위치에서 Current는 정의되지 않습니다. 따라서 MoveNext의 값을 읽기 전에 Current를 호출하여 열거자를 해당 컬렉션의 첫 번째 요소로 보내야 합니다.

Current에서는 MoveNext 또는 Reset이 호출될 때까지 동일한 개체를 반환합니다. MoveNextCurrent를 다음 요소로 설정합니다.

경우 MoveNext 열거자를 컬렉션의 끝 컬렉션의 마지막 요소 뒤에 배치 되는 전달 하 고 MoveNext 반환 false합니다. 열거자가 있는 경우이 위치에 대 한 후속 호출은 MoveNext 반환할 수도 false합니다. 반환 falseCurrent 된 에 대한 MoveNext 마지막 호출이 정의되지 않은 경우 Current를 컬렉션의 첫째 요소에 다시 설정하려면 Reset을 호출한 뒤 MoveNext를 호출해야 합니다.

컬렉션이 변경되지 않고 그대로 유지되는 한 열거자는 유효한 상태로 유지됩니다. 컬렉션에 대해 변경 내용을 수행하면(예: 요소 추가, 수정 또는 삭제) 열거자는 복구 불가능하게 무효화되고 해당 동작은 정의되지 않습니다.

열거자는 컬렉션에 배타적으로 액세스하지 못하므로 컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 열거 동안 스레드 보안을 보장하려면 전체 열거 동안 컬렉션을 잠그면 됩니다. 여러 스레드에서 컬렉션에 액세스하여 읽고 쓸 수 있도록 허용하려면 사용자 지정 동기화를 구현해야 합니다.

이 메서드는 작업입니다 O(1) .

추가 정보

적용 대상