C# | Reverse the order of the elements in the entire ArrayList or in the specified range
Last Updated :
01 Feb, 2019
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:
- 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:
CSHARP
// 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]);
}
}
}
Output:
Fifth
Fourth
Third
Second
First
Example 2:
CSHARP
// 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]);
}
}
}
Output:
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.
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:
CSHARP
// 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]);
}
}
}
Output:
First
Fourth
Third
Second
Fifth
Example 2:
CSHARP
// 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
20
Reference:
Similar Reads
C# | Reverse the order of the elements in the entire List or in the specified range List<T>.Reverse Method is used to reverse the order of the elements in the List<T> or a portion of it. There are two methods in the overload list of List<T>.Reverse Method as follows: Reverse() Reverse(Int32, Int32) Reverse() Method This method is used to reverse the order of the e
4 min read
C# | Remove a range of elements from the ArrayList 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.RemoveRange(Int32, Int32) method is used to remove a range of ele
3 min read
C# | Sort the elements in the ArrayList ArrayList.Sort Method is used to sorts the elements in the ArrayList. There are total 3 methods in the overload list of this method as follows: Sort()Sort(IComparer)Sort(Int32, Int32, IComparer)Sort() This method is used to Sort the elements in the entire ArrayList. It uses the QuickSort algorithm t
4 min read
Find element at given index after given range reversals An array consisting of N elements is given. There are several reversals we do in unique ranges[L..R]. The task is to print the element at given index.Examples: Input : arr[] : 10 20 30 40 50ranges[] = {{1, 4}, {0, 2}}Query Index = 1Output : 50Explanation : Reverse range[1..4] : 10 50 40 30 20Reverse
10 min read
Move all negative numbers to beginning and positive to end with constant extra space Given an array containing both positive and negative numbers in random order. The task is to rearrange the array elements so that all negative numbers appear before all positive numbers.Note: Given array does not contain any zeroes. Order of resultant array does not matter.Example : Input: -12, 11,
12 min read