Auf Englisch lesen

Freigeben über


ArrayList.Reverse Methode

Definition

Kehrt die Reihenfolge der Elemente in der ArrayList bzw. in einem Teil davon um.

Überlädt

Reverse()

Kehrt die Reihenfolge der Elemente in der gesamten ArrayList um.

Reverse(Int32, Int32)

Kehrt die Reihenfolge der Elemente im angegebenen Bereich um.

Reverse()

Quelle:
ArrayList.cs
Quelle:
ArrayList.cs
Quelle:
ArrayList.cs

Kehrt die Reihenfolge der Elemente in der gesamten ArrayList um.

public virtual void Reverse ();

Ausnahmen

ArrayList ist schreibgeschützt.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie die Sortierreihenfolge der Werte in einem ArrayListumgekehrt wird.

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" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList initially contains the following values:" );
      PrintValues( myAL );

      // Reverses the sort order of the values of the ArrayList.
      myAL.Reverse();

      // Displays the values of the ArrayList.
      Console.WriteLine( "After reversing:" );
      PrintValues( myAL );
   }

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


/*
This code produces the following output.

The ArrayList initially contains the following values:
   The
   quick
   brown
   fox
   jumps
   over
   the
   lazy
   dog

After reversing:
   dog
   lazy
   the
   over
   jumps
   fox
   brown
   quick
   The
*/

Hinweise

Diese Methode verwendetArray.Reverse, um die Reihenfolge der Elemente umzukehren, sodass das Element bei ArrayList [i], wobei i ein beliebiger Index innerhalb des Bereichs + countindexindex + ist, zu ArrayList [j], wobei j gleich - i - 1 ist.

Diese Methode ist ein O(n) Vorgang, wobei n ist Count.

Gilt für:

Reverse(Int32, Int32)

Quelle:
ArrayList.cs
Quelle:
ArrayList.cs
Quelle:
ArrayList.cs

Kehrt die Reihenfolge der Elemente im angegebenen Bereich um.

public virtual void Reverse (int index, int count);

Parameter

index
Int32

Der nullbasierte Startindex des Bereichs, in dem die Reihenfolge umgekehrt werden soll.

count
Int32

Die Anzahl der Elemente im Bereich, in dem die Reihenfolge umgekehrt werden soll.

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.

ArrayList ist schreibgeschützt.

Beispiele

Das folgende Codebeispiel zeigt, wie die Sortierreihenfolge der Werte in einem -Elementbereich in umgekehrt wird ArrayList.

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

   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" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList initially contains the following values:" );
      PrintValues( myAL );

      // Reverses the sort order of the values of the ArrayList.
      myAL.Reverse( 1, 3 );

      // Displays the values of the ArrayList.
      Console.WriteLine( "After reversing:" );
      PrintValues( myAL );
   }

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


/*
This code produces the following output.

The ArrayList initially contains the following values:
   The
   QUICK
   BROWN
   FOX
   jumps
   over
   the
   lazy
   dog

After reversing:
   The
   FOX
   BROWN
   QUICK
   jumps
   over
   the
   lazy
   dog

*/

Hinweise

Diese Methode verwendetArray.Reverse, um die Reihenfolge der Elemente umzukehren, sodass das Element bei ArrayList [i], wobei i ein beliebiger Index innerhalb des Bereichs + countindexindex + ist, zu ArrayList [j], wobei j gleich - i - 1 ist.

Diese Methode ist ein O(n) Vorgang, wobei n ist count.

Gilt für: