ArrayList.Reverse Method (Int32, Int32)
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| index is less than zero. -or- count is less than zero. | |
| index and count do not denote a valid range of elements in the ArrayList. | |
| 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.
using namespace System; using namespace System::Collections; void PrintValues( IEnumerable^ myList ); int main() { // Creates and initializes a new ArrayList. ArrayList^ myAL = gcnew 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 ); } void PrintValues( IEnumerable^ myList ) { IEnumerator^ myEnum = myList->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ obj = safe_cast<Object^>(myEnum->Current); Console::WriteLine( " {0}", obj ); } Console::WriteLine(); } /* 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 */
import System.*;
import System.Collections.*;
public class SamplesArrayList
{
public static void main(String[] args)
{
// Creates and initializes a new ArrayList.
ArrayList myAL = 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);
} //main
public static void PrintValues(IEnumerable myList)
{
IEnumerator objMyEnum = myList.GetEnumerator();
while (objMyEnum.MoveNext()) {
Object obj = objMyEnum.get_Current();
Console.WriteLine(" {0}", obj);
}
Console.WriteLine();
} //PrintValues
} //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
*/
import System; import System.Collections; // Creates and initializes a new ArrayList. var myAL : ArrayList = new ArrayList(); myAL.Add( "The" ); myAL.Add( "QUICK" ); myAL.Add( "BROWN" ); myAL.Add( "FOX" ); myAL.Add( "jumped" ); 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:" ); PrintIndexAndValues( 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:" ); PrintIndexAndValues( myAL ); function PrintIndexAndValues( myList : IEnumerable ) { var i : int = 0; var myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current ); Console.WriteLine(); } /* This code produces the following output. The ArrayList 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.