Прочитать на английском

Поделиться через


ArrayList.GetRange(Int32, Int32) Метод

Определение

Возвращает список ArrayList, представляющий подмножество элементов исходного списка ArrayList.

public virtual System.Collections.ArrayList GetRange (int index, int count);

Параметры

index
Int32

Отсчитываемый от нуля индекс списка ArrayList, с которого начинается диапазон.

count
Int32

Число элементов в диапазоне.

Возвращаемое значение

Список ArrayList, представляющий подмножество элементов исходного списка ArrayList.

Исключения

Значение параметра index меньше нуля.

-или-

Значение параметра count меньше нуля.

Параметры index и count не указывают допустимый диапазон элементов в списке 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
*/

Комментарии

Этот метод не создает копии элементов. Новый ArrayList — это только окно представления в источнике ArrayList. Однако все последующие изменения в источнике ArrayList должны выполняться в этом окне ArrayListпредставления . Если изменения вносятся непосредственно в источник ArrayList, окно ArrayList представления становится недействительным, и все операции с ним возвращают .InvalidOperationException

Этот метод является операцией O(1) .

Применяется к

Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

См. также раздел