Export (0) Print
Expand All

ArrayList.Reverse Method

Reverses the order of the elements in the entire ArrayList.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Overridable Sub Reverse

ExceptionCondition
NotSupportedException

The ArrayList is read-only.

This method uses Array.Reverse to reverse the order of the elements, such that the element at ArrayList [i], where i is any index within the range, moves to ArrayList [j], where j equals index + index + count - i - 1.

This method is an O(n) operation, where n is Count.

The following code example shows how to reverse the sort order of the values in an ArrayList.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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 'PrintValues

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft