ArrayList.Reverse 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 ArrayList 或它的一部分中元素的顺序反转。
重载
| Reverse() | 将整个 ArrayList 中元素的顺序反转。 | 
| Reverse(Int32, Int32) | 将指定范围中元素的顺序反转。 | 
Reverse()
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
将整个 ArrayList 中元素的顺序反转。
public:
 virtual void Reverse();public virtual void Reverse ();abstract member Reverse : unit -> unit
override this.Reverse : unit -> unitPublic Overridable Sub Reverse ()例外
示例
下面的代码示例演示如何反转 中 ArrayList值的排序顺序。
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew 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 );
}
void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      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
 */
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
*/
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")
        
        ' 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)
    End Sub
    
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub
End Class
' 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
注解
此方法使用 Array.Reverse 反转元素的顺序,使 [i] 处ArrayList的元素(其中 i 是范围内的任何索引)移动到 ArrayList [j],其中 j 等于countindex + index + - i - 1。
此方法是一个 O(n) 操作,其中 n 是 Count。
适用于
Reverse(Int32, Int32)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
将指定范围中元素的顺序反转。
public:
 virtual void Reverse(int index, int count);public virtual void Reverse (int index, int count);abstract member Reverse : int * int -> unit
override this.Reverse : int * int -> unitPublic Overridable Sub Reverse (index As Integer, count As Integer)参数
- index
- Int32
要反转的范围的从零开始的起始索引。
- count
- Int32
要反转的范围内的元素数。
例外
              index 和 count 不表示 ArrayList 中元素的有效范围。
示例
下面的代码示例演示如何反转 中元素范围中的 ArrayList值的排序顺序。
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew 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 );
}
void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      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
 */
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
*/
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")
        ' 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)
    End Sub
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub
End Class
' 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
注解
此方法使用 Array.Reverse 反转元素的顺序,使 [i] 处ArrayList的元素(其中 i 是范围内的任何索引)移动到 ArrayList [j],其中 j 等于countindex + index + - i - 1。
此方法是一个 O(n) 操作,其中 n 是 count。