课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - SortedList

当前位置:免费教程 » 程序设计 » VB.Net
SortedList类表示按键排序并可通过键和索引访问的键 - 值对的集合。

排序列表是数组和散列表的组合。 它包含可以使用键或索引访问的项目列表。 如果您使用索引访问项目,它是一个ArrayList,如果您使用键访问项目,它是一个Hashtable。 项目的集合始终按键值排序。


SortedList类的属性和方法

下表列出了SortedList类的一些常用属性:
属性描述
CapacityGets or sets the capacity of the SortedList.
获取或设置SortedList的容量。
CountGets the number of elements contained in the SortedList.
获取SortedList中包含的元素数。
IsFixedSizeGets a value indicating whether the SortedList has a fixed size.
获取指示SortedList是否具有固定大小的值。
IsReadOnlyGets a value indicating whether the SortedList is read-only.
获取指示SortedList是否为只读的值。
ItemGets and sets the value associated with a specific key in the SortedList.
获取并设置与SortedList中的特定键相关联的值。
KeysGets the keys in the SortedList.
获取SortedList中的键。
ValuesGets the values in the SortedList.
获取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中的实际元素数。


示例:

下面的例子演示了这个概念:
  1. Module collections
  2. Sub Main()
  3. Dim sl As SortedList = New SortedList()
  4. sl.Add("001", "Zara Ali")
  5. sl.Add("002", "Abida Rehman")
  6. sl.Add("003", "Joe Holzner")
  7. sl.Add("004", "Mausam Benazir Nur")
  8. sl.Add("005", "M. Amlan")
  9. sl.Add("006", "M. Arif")
  10. sl.Add("007", "Ritesh Saikia")
  11. If (sl.ContainsValue("Nuha Ali")) Then
  12. Console.WriteLine("This student name is already in the list")
  13. Else
  14. sl.Add("008", "Nuha Ali")
  15. End If
  16. ' Get a collection of the keys.
  17. Dim key As ICollection = sl.Keys
  18. Dim k As String
  19. For Each k In key
  20. Console.WriteLine(" {0} : {1}", k, sl(k))
  21. Next k
  22. Console.ReadKey()
  23. End Sub
  24. End Module

当上述代码被编译和执行时,它产生以下结果:
  1. 001: Zara Ali
  2. 002: Abida Rehman
  3. 003: Joe Holzner
  4. 004: Mausam Banazir Nur
  5. 005: M. Amlan
  6. 006: M. Arif
  7. 007: Ritesh Saikia
  8. 008: Nuha Ali

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号