ArrayList.Reverse Method (Int32, Int32)

Reverses the order of the elements in the specified range.

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

'Declaration
Public Overridable Sub Reverse ( _
	index As Integer, _
	count As Integer _
)
'Usage
Dim instance As ArrayList 
Dim index As Integer 
Dim count As Integer

instance.Reverse(index, count)

Parameters

index
Type: System.Int32

The zero-based starting index of the range to reverse.

count
Type: System.Int32

The number of elements in the range to reverse.

ExceptionCondition
ArgumentOutOfRangeException

index is less than zero.

-or-

count is less than zero.

ArgumentException

index and count do not denote a valid range of elements in the ArrayList.

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 a range of elements in an ArrayList.

Imports System
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 'Main

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


' 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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft