Array.Reverse Method (Array, Int32, Int32)
.NET Framework 3.0
Reverses the sequence of the elements in a range of elements in the one-dimensional Array.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
public static void Reverse ( Array array, int index, int length )
public static function Reverse ( array : Array, index : int, length : int )
Not applicable.
Parameters
- array
The one-dimensional Array to reverse.
- index
The starting index of the section to reverse.
- length
The number of elements in the section to reverse.
The following code example shows how to reverse the sort of the values in a range of elements in an Array.
using System; public class SamplesArray { public static void Main() { // 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. Console.WriteLine( "The Array initially contains the following values:" ); PrintIndexAndValues( myArray ); // Reverses the sort of the values of the Array. Array.Reverse( myArray, 1, 3 ); // Displays the values of the Array. Console.WriteLine( "After reversing:" ); PrintIndexAndValues( myArray ); } public static void PrintIndexAndValues( Array myArray ) { for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ ) Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) ); } } /* 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 */
import System.*;
public class SamplesArray
{
public static void main(String[] args)
{
// Creates and initializes a new Array.
Array myArray = Array.CreateInstance(String.class.ToType(), 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.
Console.WriteLine("The Array initially contains the following values:");
PrintIndexAndValues(myArray);
// Reverses the sort of the values of the Array.
Array.Reverse(myArray, 1, 3);
// Displays the values of the Array.
Console.WriteLine("After reversing:");
PrintIndexAndValues(myArray);
} //main
public static void PrintIndexAndValues(Array myArray)
{
for (int i = myArray.GetLowerBound(0); i <= myArray.
GetUpperBound(0); i++) {
Console.WriteLine("\t[{0}]:\t{1}", (Int32)i, myArray.GetValue(i));
}
} //PrintIndexAndValues
} //SamplesArray
/*
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
*/
import System; // Creates and initializes a new Array. var myArray : System.Array= System.Array.CreateInstance( System.String, 9 ); myArray.SetValue( "The", 0 ); myArray.SetValue( "QUICK", 1 ); myArray.SetValue( "BROWN", 2 ); myArray.SetValue( "FOX", 3 ); myArray.SetValue( "jumped", 4 ); myArray.SetValue( "over", 5 ); myArray.SetValue( "the", 6 ); myArray.SetValue( "lazy", 7 ); myArray.SetValue( "dog", 8 ); // Displays the values of the Array. Console.WriteLine( "The Array initially contains the following values:" ); PrintIndexAndValues( myArray ); // Reverses the sort of the values of the Array. System.Array.Reverse( myArray, 1, 3 ); // Displays the values of the Array. Console.WriteLine( "After reversing:" ); PrintIndexAndValues( myArray ); function PrintIndexAndValues( myArray : System.Array ) { for ( var i : int = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ ) Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) ); } /* This code produces the following output. The Array initially contains the following values: [0]: The [1]: QUICK [2]: BROWN [3]: FOX [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog After reversing: [0]: The [1]: FOX [2]: BROWN [3]: QUICK [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog */
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.