ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Reverse Method is used to reverse the order of the elements in the ArrayList in the specified range. There are two methods in the overload list of ArrayList.Reverse Method as follows:
CSHARP
Output:
CSHARP
Output:
CSHARP
Output:
CSHARP
- ArrayList.Reverse()
- ArrayList.Reverse(Int32, Int32)
ArrayList.Reverse() Method
This method is used to reverse the order of the elements in the entire ArrayList. Syntax:public virtual void Reverse ();Exception: This method will give NotSupportedException if the ArrayList is read-only. Note: This method is an O(n) operation, where n is Count. Below given are some examples to understand the implementation in a better way: Example 1:
// C# code to reverse the order of
// the elements in the entire ArrayList
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an ArrayList
ArrayList myList = new ArrayList(5);
// Adding elements to ArrayList
myList.Add("First");
myList.Add("Second");
myList.Add("Third");
myList.Add("Fourth");
myList.Add("Fifth");
// Reversing the order of elements in entire ArrayList
myList.Reverse();
// Displaying the elements in myList
for (int i = 0; i < myList.Count; i++) {
Console.WriteLine(myList[i]);
}
}
}
Fifth Fourth Third Second FirstExample 2:
// C# code to reverse the order of
// the elements in the entire ArrayList
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an ArrayList
ArrayList myList = new ArrayList(5);
// Adding elements to ArrayList
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);
myList.Add(5);
// Reversing the order of elements in entire ArrayList
myList.Reverse();
// Displaying the elements in myList
for (int i = 0; i < myList.Count; i++) {
Console.WriteLine(myList[i]);
}
}
}
5 4 3 2 1
ArrayList.Reverse(Int32, Int32)
This method is used to reverse the order of the elements in the specified range. Syntax:public virtual void Reverse (int index, int count);Parameters:
index: It is the zero-based starting index of the range which is to be reversed. count: It is the number of the elements in the range which is to be reversed.Exceptions:
- ArgumentOutOfRangeException : If the index is less than zero or count is less than zero.
- ArgumentException : If index and count do not denote a valid range of elements in the ArrayList.
- NotSupportedException : If the ArrayList is read-only or the ArrayList has a fixed size.
// C# code to reverse a sub
// range in the ArrayList
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main() {
// Creating an ArrayList
ArrayList myList = new ArrayList(5);
// Adding elements to ArrayList
myList.Add("First");
myList.Add("Second");
myList.Add("Third");
myList.Add("Fourth");
myList.Add("Fifth");
// Reversing the sub-range
// starting from index 1, and
// count 3 elements
myList.Reverse(1, 3);
// Displaying the elements in myList
for (int i = 0; i < myList.Count; i++) {
Console.WriteLine(myList[i]);
}
}
}
First Fourth Third Second FifthExample 2:
// C# code to reverse a sub
// range in the ArrayList
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main() {
// Creating an ArrayList
ArrayList myList = new ArrayList(5);
// Adding elements to ArrayList
myList.Add(4);
myList.Add(8);
myList.Add(12);
myList.Add(16);
myList.Add(20);
// Reversing the sub-range
// starting from index 0, and
// count 2 elements
myList.Reverse(0, 2);
// Displaying the elements in myList
for (int i = 0; i < myList.Count; i++) {
Console.WriteLine(myList[i]);
}
}
}
Output:
8 4 12 16 20Reference: