ArrayList表示可以单独索引的对象的有序集合。基本上,它是数组的替代方法。它还允许动态分配内存,添加,搜索和排序列表中的项目。 ArrayList.Reverse方法用于在指定范围内反转ArrayList中元素的顺序。 ArrayList.Reverse方法的重载列表中有两种方法,如下所示:
- ArrayList.Reverse()
- ArrayList.Reverse(Int32,Int32)
ArrayList.Reverse()方法
此方法用于反转整个ArrayList中元素的顺序。
句法:
public virtual void Reverse ();
异常:如果ArrayList为只读,则此方法将提供NotSupportedException。
注意:此方法是O(n)运算,其中n是Count。
下面给出了一些示例,以更好地理解实现:
范例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
First
范例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)
此方法用于反转指定范围内元素的顺序。
句法:
public virtual void Reverse (int index, int count);
参数:
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.
例外情况:
- ArgumentOutOfRangeException:如果索引小于零或count小于零。
- ArgumentException:如果index和count不表示ArrayList中元素的有效范围。
- NotSupportedException:如果ArrayList为只读或ArrayList具有固定大小。
注意:此方法是O(n)运算,其中n是Count。
下面给出了一些示例,以更好地理解实现:
范例1:
// 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
Fifth
范例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]);
}
}
}
输出:
8
4
12
16
20
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist.reverse?view=netframework-4.7.2