排序列表是数组和散列表的组合。 它包含可以使用键或索引访问的项目列表。 如果您使用索引访问项目,它是一个ArrayList,如果您使用键访问项目,它是一个Hashtable。 项目的集合始终按键值排序。
SortedList类的属性和方法
属性 | 描述 |
---|---|
Capacity | Gets or sets the capacity of the SortedList. 获取或设置SortedList的容量。 |
Count | Gets the number of elements contained in the SortedList. 获取SortedList中包含的元素数。 |
IsFixedSize | Gets a value indicating whether the SortedList has a fixed size. 获取指示SortedList是否具有固定大小的值。 |
IsReadOnly | Gets a value indicating whether the SortedList is read-only. 获取指示SortedList是否为只读的值。 |
Item | Gets and sets the value associated with a specific key in the SortedList. 获取并设置与SortedList中的特定键相关联的值。 |
Keys | Gets the keys in the SortedList. 获取SortedList中的键。 |
Values | Gets the values in the SortedList. 获取SortedList中的值。 |
S.N | 方法名称和用途 |
---|---|
1 | Public Overridable Sub Add (key As Object, value As Object) Adds an element with the specified key and value into the SortedList. 将带有指定的键和值的元素添加到可排序列表。 |
2 | Public Overridable Sub Clear Removes all elements from the SortedList.、 从SortedList中删除所有元素。 |
3 | Public Overridable Function ContainsKey (key As Object) As Boolean Determines whether the SortedList contains a specific key. 确定SortedList是否包含特定键。 |
4 | Public Overridable Function ContainsValue (value As Object) As Boolean Determines whether the SortedList contains a specific value. 确定SortedList是否包含特定值。 |
5 | Public Overridable Function GetByIndex (index As Integer) As Object Gets the value at the specified index of the SortedList. 获取SortedList的指定索引处的值。 |
6 | Public Overridable Function GetKey (index As Integer) As Object Gets the key at the specified index of the SortedList. 获取SortedList的指定索引处的键。 |
7 | Public Overridable Function GetKeyList As IList Gets the keys in the SortedList. 获取SortedList中的键。 |
8 | Public Overridable Function GetValueList As IList Gets the values in the SortedList. 获取SortedList中的值。 |
9 | Public Overridable Function IndexOfKey (key As Object) As Integer Returns the zero-based index of the specified key in the SortedList. 返回SortedList中指定键的从零开始的索引。 |
10 | Public Overridable Function IndexOfValue (value As Object ) As Integer Returns the zero-based index of the first occurrence of the specified value in the SortedList. 返回SortedList中指定值的第一次出现的从零开始的索引。 |
11 | Public Overridable Sub Remove (key As Object) Removes the element with the specified key from the SortedList. 从SortedList中删除具有指定键的元素。 |
12 | Public Overridable Sub RemoveAt (index As Integer) Removes the element at the specified index of SortedList. 删除SortedList的指定索引处的元素。 |
13 | Public Overridable Sub TrimToSize Sets the capacity to the actual number of elements in the SortedList. 将容量设置为SortedList中的实际元素数。 |
示例:
- Module collections
- Sub Main()
- Dim sl As SortedList = New SortedList()
- sl.Add("001", "Zara Ali")
- sl.Add("002", "Abida Rehman")
- sl.Add("003", "Joe Holzner")
- sl.Add("004", "Mausam Benazir Nur")
- sl.Add("005", "M. Amlan")
- sl.Add("006", "M. Arif")
- sl.Add("007", "Ritesh Saikia")
- If (sl.ContainsValue("Nuha Ali")) Then
- Console.WriteLine("This student name is already in the list")
- Else
- sl.Add("008", "Nuha Ali")
- End If
- ' Get a collection of the keys.
- Dim key As ICollection = sl.Keys
- Dim k As String
- For Each k In key
- Console.WriteLine(" {0} : {1}", k, sl(k))
- Next k
- Console.ReadKey()
- End Sub
- End Module
- 001: Zara Ali
- 002: Abida Rehman
- 003: Joe Holzner
- 004: Mausam Banazir Nur
- 005: M. Amlan
- 006: M. Arif
- 007: Ritesh Saikia
- 008: Nuha Ali
转载本站内容时,请务必注明来自W3xue,违者必究。