Auf Englisch lesen

Teilen über


ArrayList.GetRange(Int32, Int32) Methode

Definition

Gibt eine ArrayList zurück, die eine Teilgruppe der Elemente aus der Quell-ArrayList darstellt.

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

Parameter

index
Int32

Der nullbasierte ArrayList-Index, an dem der Bereich beginnt.

count
Int32

Die Anzahl der Elemente im Bereich.

Gibt zurück

Eine ArrayList, die eine Teilgruppe der Elemente aus der Quell-ArrayList darstellt.

Ausnahmen

index ist kleiner als Null.

- oder -

count ist kleiner als Null.

index und count geben keinen gültigen Bereich von Elementen in der ArrayList an.

Beispiele

Das folgende Codebeispiel zeigt, wie Sie einen Bereich von Elementen in festlegen ArrayListund abrufen.

C#
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
*/

Hinweise

Diese Methode erstellt keine Kopien der Elemente. Das neue ArrayList ist nur ein Ansichtsfenster in der Quelle ArrayList. Alle nachfolgenden Änderungen an der Quelle ArrayList müssen jedoch über dieses Ansichtsfenster ArrayListerfolgen. Wenn Änderungen direkt an der Quelle ArrayListvorgenommen werden, wird das Ansichtsfenster ArrayList ungültig, und alle Vorgänge darauf geben einen InvalidOperationExceptionzurück.

Diese Methode ist ein O(1) Vorgang.

Gilt für:

Produkt Versionen
.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

Weitere Informationen