Export (0) Print
Expand All

Array.Reverse Method (Array, Int32, Int32)

Reverses the sequence of the elements in a range of elements in the one-dimensional Array.

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

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Sub Reverse ( _
	array As Array, _
	index As Integer, _
	length As Integer _
)

Parameters

array
Type: System.Array
The one-dimensional Array to reverse.
index
Type: System.Int32
The starting index of the section to reverse.
length
Type: System.Int32
The number of elements in the section to reverse.

ExceptionCondition
ArgumentNullException

array is Nothing.

RankException

array is multidimensional.

ArgumentOutOfRangeException

index is less than the lower bound of array.

-or-

length is less than zero.

ArgumentException

index and length do not specify a valid range in array.

After a call to this method, the element at myArray[i], where i is any index in the array, moves to myArray[j], where j equals (myArray.Length + myArray.GetLowerBound(0)) - (i - myArray.GetLowerBound(0)) - 1.

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

The following code example shows how to reverse the sort of the values in a range of elements in an Array.



Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Creates and initializes a new Array.
      Dim myArray As Array = Array.CreateInstance(GetType(String), 9)
      myArray.SetValue("The", 0)
      myArray.SetValue("QUICK", 1)
      myArray.SetValue("BROWN", 2)
      myArray.SetValue("FOX", 3)
      myArray.SetValue("jumps", 4)
      myArray.SetValue("over", 5)
      myArray.SetValue("the", 6)
      myArray.SetValue("lazy", 7)
      myArray.SetValue("dog", 8)

      ' Displays the values of the Array.
      outputBlock.Text &= "The Array initially contains the " _
         + "following values:" & vbCrLf
      PrintIndexAndValues(outputBlock, myArray)

      ' Reverses the sort of the values of the Array.
      Array.Reverse(myArray, 1, 3)

      ' Displays the values of the Array.
      outputBlock.Text &= "After reversing:" & vbCrLf
      PrintIndexAndValues(outputBlock, myArray)
   End Sub


   Public Shared Sub PrintIndexAndValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myArray As Array)
      Dim i As Integer
      For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
         outputBlock.Text &= String.Format(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
            + "{1}", i, myArray.GetValue(i)) & vbCrLf
      Next i
   End Sub
End Class

' This code produces the following output.
' 
' The Array initially contains the following values:
'     [0]:    The
'     [1]:    QUICK
'     [2]:    BROWN
'     [3]:    FOX
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
' After reversing:
'     [0]:    The
'     [1]:    FOX
'     [2]:    BROWN
'     [3]:    QUICK
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog 


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft