SortedList类是根据键对(键,值)对进行排序的集合。可以通过键和索引(从零开始的索引)访问这些对。它位于System.Collections命名空间下。 SortedList.RemoveAt(Int32)方法用于删除SortedList对象指定索引处的元素。
特性:
- 可以通过其键或索引来访问SortedList元素。
- SortedList对象在内部维护两个数组来存储列表的元素,即,一个数组用于键,另一个数组用于关联的值。
- 键不能为null,但值可以为null。
- SortedList对象的容量是SortedList可以容纳的元素数。
- SortedList不允许重复的键。
- 由于排序,对SortedList对象的操作往往比对Hashtable对象的操作要慢。
- 可以使用整数索引访问此集合中的元素。此集合中的索引从零开始。
句法:
public virtual void RemoveAt (int index);
在这里, index是要删除的元素的从零开始的索引。
例外情况:
- NotSupportedException:如果SortedList对象是只读的,或者SortedList具有固定的大小。
- ArgumentOutOfRangeException:如果索引超出SortedList对象的有效索引范围。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to remove the element at
// the specified index of a SortedList
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an SortedList
SortedList mySortedList = new SortedList();
// Adding elements to SortedList
mySortedList.Add("DS", "Data Structures");
mySortedList.Add("EE", "Electrical Engineering");
mySortedList.Add("CS", "Computer Science");
mySortedList.Add("ME", "Mechanical Engineering");
mySortedList.Add("CE", "Civil Engineering");
// Displaying elements in SortedList
foreach(string mykey in mySortedList.Keys)
Console.WriteLine("Key = " + mykey);
foreach(string myvalue in mySortedList.Values)
Console.WriteLine("Key = " + myvalue);
// Removing element at index 4
Console.WriteLine("Removing element at index 4");
mySortedList.RemoveAt(4);
// Displaying elements in SortedList
foreach(string mykey in mySortedList.Keys)
Console.WriteLine("Key = " + mykey);
foreach(string myvalue in mySortedList.Values)
Console.WriteLine("Key = " + myvalue);
}
}
输出:
Key = CE
Key = CS
Key = DS
Key = EE
Key = ME
Key = Civil Engineering
Key = Computer Science
Key = Data Structures
Key = Electrical Engineering
Key = Mechanical Engineering
Removing element at index 4
Key = CE
Key = CS
Key = DS
Key = EE
Key = Civil Engineering
Key = Computer Science
Key = Data Structures
Key = Electrical Engineering
范例2:
// C# code to remove the element at
// the specified index of a SortedList
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an SortedList
SortedList mySortedList = new SortedList();
// Adding elements to SortedList
mySortedList.Add("DS", "Data Structures");
mySortedList.Add("EE", "Electrical Engineering");
mySortedList.Add("CS", "Computer Science");
mySortedList.Add("ME", "Mechanical Engineering");
mySortedList.Add("CE", "Civil Engineering");
// Displaying elements in SortedList
foreach(string mykey in mySortedList.Keys)
Console.WriteLine("Key = " + mykey);
foreach(string myvalue in mySortedList.Values)
Console.WriteLine("Key = " + myvalue);
// Removing element at index 8
Console.WriteLine("Removing element at index 8");
// It should raise ArgumentOutOfRangeException
// As index is outside the range of valid
// indexes for the SortedList object.
mySortedList.RemoveAt(8);
// Displaying elements in SortedList
foreach(string mykey in mySortedList.Keys)
Console.WriteLine("Key = " + mykey);
foreach(string myvalue in mySortedList.Values)
Console.WriteLine("Key = " + myvalue);
}
}
错误:
Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
笔记:
- 索引序列基于排序序列。添加元素后,它会以正确的排序顺序插入SortedList中,索引也会相应调整。删除元素后,索引也会相应调整。因此,随着从SortedList对象中添加或删除元素,特定键/值对的索引可能会更改。
- 此方法是O(n)运算,其中n是Count。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.sortedlist.removeat?view=netframework-4.7.2