Condividi tramite


Array.Reverse Method (Array)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Reverses the sequence of the elements in the entire one-dimensional Array.

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

Syntax

'Declaration
Public Shared Sub Reverse ( _
    array As Array _
)
public static void Reverse(
    Array array
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

array is nulla null reference (Nothing in Visual Basic).

RankException

array is multidimensional.

Remarks

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 the Length of array.

Examples

The following code example shows how to reverse the sort of the values 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)

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



   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]:    dog
'     [1]:    lazy
'     [2]:    the
'     [3]:    over
'     [4]:    jumps
'     [5]:    fox
'     [6]:    brown
'     [7]:    quick
'     [8]:    The 
using System;
public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Creates and initializes a new Array.
      Array myArray = Array.CreateInstance(typeof(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:" + "\n";
      PrintIndexAndValues(outputBlock, myArray);

      // Reverses the sort of the values of the Array.
      Array.Reverse(myArray);

      // Displays the values of the Array.
      outputBlock.Text += "After reversing:" + "\n";
      PrintIndexAndValues(outputBlock, myArray);
   }


   public static void PrintIndexAndValues(System.Windows.Controls.TextBlock outputBlock, Array myArray)
   {
      for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
         outputBlock.Text += String.Format("\t[{0}]:\t{1}", i, myArray.GetValue(i)) + "\n";
   }
}
/* 
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]:    dog
    [1]:    lazy
    [2]:    the
    [3]:    over
    [4]:    jumps
    [5]:    fox
    [6]:    brown
    [7]:    quick
    [8]:    The
*/

Version Information

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

Platforms

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