ArrayList.GetRange(Int32, Int32) 메서드

정의

소스ArrayListArrayList 있는 요소의 하위 집합을 나타내는 값을 반환합니다.

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

매개 변수

index
Int32

범위가 시작되는 인덱스(0부터 ArrayList 시작)입니다.

count
Int32

범위의 요소 수입니다.

반품

ArrayList 소스ArrayList에 있는 요소의 하위 집합을 나타내는 값입니다.

예외

index가 0보다 작습니다.

-또는-

count가 0보다 작습니다.

indexcount 있는 유효한 요소 ArrayList범위를 나타내지 않습니다.

예제

다음 코드 예제에서는 설정 하 고 요소의 범위를 가져오는 방법을 보여 있습니다 ArrayList.

using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Creates and initializes the source ICollection.
      Queue mySourceList = new Queue();
      mySourceList.Enqueue( "big" );
      mySourceList.Enqueue( "gray" );
      mySourceList.Enqueue( "wolf" );

      // Displays the values of five elements starting at index 0.
      ArrayList mySubAL = myAL.GetRange( 0, 5 );
      Console.WriteLine( "Index 0 through 4 contains:" );
      PrintValues( mySubAL, '\t' );

      // Replaces the values of five elements starting at index 1 with the values in the ICollection.
      myAL.SetRange( 1, mySourceList );

      // Displays the values of five elements starting at index 0.
      mySubAL = myAL.GetRange( 0, 5 );
      Console.WriteLine( "Index 0 through 4 now contains:" );
      PrintValues( mySubAL, '\t' );
   }

   public static void PrintValues( IEnumerable myList, char mySeparator )  {
      foreach ( Object obj in myList )
         Console.Write( "{0}{1}", mySeparator, obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

Index 0 through 4 contains:
        The     quick   brown   fox     jumps
Index 0 through 4 now contains:
        The     big     gray    wolf    jumps
*/
Imports System.Collections

Public Class SamplesArrayList

    Public Shared Sub Main()

        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("quick")
        myAL.Add("brown")
        myAL.Add("fox")
        myAL.Add("jumps")
        myAL.Add("over")
        myAL.Add("the")
        myAL.Add("lazy")
        myAL.Add("dog")

        ' Creates and initializes the source ICollection.
        Dim mySourceList As New Queue()
        mySourceList.Enqueue("big")
        mySourceList.Enqueue("gray")
        mySourceList.Enqueue("wolf")

        ' Displays the values of five elements starting at index 0.
        Dim mySubAL As ArrayList = myAL.GetRange(0, 5)
        Console.WriteLine("Index 0 through 4 contains:")
        PrintValues(mySubAL, vbTab)

        ' Replaces the values of five elements starting at index 1 with the values in the ICollection.
        myAL.SetRange(1, mySourceList)

        ' Displays the values of five elements starting at index 0.
        mySubAL = myAL.GetRange(0, 5)
        Console.WriteLine("Index 0 through 4 now contains:")
        PrintValues(mySubAL, vbTab)

    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("{0}{1}", mySeparator, obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' Index 0 through 4 contains:
'         The     quick   brown   fox     jumps
' Index 0 through 4 now contains:
'         The     big     gray    wolf    jumps

설명

이 메서드는 요소의 복사본을 만들지 않습니다. 새 ArrayList 창은 원본 ArrayList에 대한 보기 창일 뿐입니다. 그러나 원본 ArrayList 에 대한 모든 후속 변경은 이 보기 창을 ArrayList통해 수행해야 합니다. 원본 ArrayList을 직접 변경하면 보기 창이 무효화되고 뷰 창 ArrayList 에 대한 모든 작업이 반환 InvalidOperationException됩니다.

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

적용 대상

추가 정보