SortedList类是根据键对(键,值)对进行排序的集合。可以通过键和索引(从零开始的索引)访问这些对。它位于System.Collections命名空间下。 SortedList.Remove(Object)方法用于从SortedList对象中删除具有指定键的元素。
特性:
- 可以通过其键或索引来访问SortedList元素。
- SortedList对象在内部维护两个数组来存储列表的元素,即,一个数组用于键,另一个数组用于关联的值。
- 键不能为null,但值可以为null。
- SortedList对象的容量是SortedList可以容纳的元素数。
- SortedList不允许重复的键。
- 由于排序,对SortedList对象的操作往往比对Hashtable对象的操作要慢。
- 可以使用整数索引访问此集合中的元素。此集合中的索引从零开始。
句法 :
public virtual void Remove (object key);
在此, key是要删除的元素的键。
例外情况:
- NotSupportedException:如果SortedList对象是只读的,或者SortedList具有固定的大小。
- ArgumentNullException:如果键为null。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to remove the element
// with the specified key from 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("IN", "INDIA");
mySortedList.Add("NY", "NEW-YORK");
mySortedList.Add("UK", "UNITED KINGDOM");
mySortedList.Add("GER", "GERMANY");
mySortedList.Add("CH", "CHINA");
// 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 having key as "UK"
Console.WriteLine("Removing element having key as UK");
mySortedList.Remove("UK");
// 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 = CH
Key = GER
Key = IN
Key = NY
Key = UK
Key = CHINA
Key = GERMANY
Key = INDIA
Key = NEW-YORK
Key = UNITED KINGDOM
Removing element having key as UK
Key = CH
Key = GER
Key = IN
Key = NY
Key = CHINA
Key = GERMANY
Key = INDIA
Key = NEW-YORK
范例2:
// C# code to remove the element
// with the specified key from a SortedList
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating an SortedList
SortedList mySortedList = new SortedList();
// Adding elements to SortedList
mySortedList.Add("IN", "INDIA");
mySortedList.Add("NY", "NEW-YORK");
mySortedList.Add("UK", "UNITED KINGDOM");
mySortedList.Add("GER", "GERMANY");
mySortedList.Add("CH", "CHINA");
// 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 having key as null
Console.WriteLine("Removing element having key as null");
// It should raise ArgumentNullException
// as key can not be null
mySortedList.Remove(null);
// 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.ArgumentNullException: Key cannot be null.
Parameter name: key
笔记:
- 如果SortedList对象不包含具有指定键的元素,则SortedList保持不变。没有异常被抛出。
- 此方法是O(n)运算,其中n是Count。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.sortedlist.remove?view=netframework-4.7.2