目录

SortedList

SortedList类表示按键排序的键 - 值对的集合,可通过键和索引访问。

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

SortedList类的属性和方法

下表列出了SortedList类的一些常用properties -

Sr.No 财产和描述
1

Capacity

获取或设置SortedList的容量。

2

Count

获取SortedList中包含的元素数。

3

IsFixedSize

获取一个值,该值指示SortedList是否具有固定大小。

4

IsReadOnly

获取一个值,该值指示SortedList是否为只读。

5

Item

获取并设置与SortedList中的特定键关联的值。

6

Keys

获取SortedList中的键。

7

Values

获取SortedList中的值。

下表列出了SortedList类的一些常用methods -

Sr.No. 方法名称和目的
1

Public Overridable Sub Add (key As Object, value As Object)

将具有指定键和值的元素添加到SortedList中。

2

Public Overridable Sub Clear

从SortedList中删除所有元素。

3

Public Overridable Function ContainsKey (key As Object) As Boolean

确定SortedList是否包含特定键。

4

Public Overridable Function ContainsValue (value As Object) As Boolean

确定SortedList是否包含特定值。

5

Public Overridable Function GetByIndex (index As Integer) As Object

获取SortedList的指定索引处的值。

6

Public Overridable Function GetKey (index As Integer) As Object

获取SortedList的指定索引处的键。

7

Public Overridable Function GetKeyList As IList

获取SortedList中的键。

8

Public Overridable Function GetValueList As IList

获取SortedList中的值。

9

Public Overridable Function IndexOfKey (key As Object) As Integer

返回SortedList中指定键的从零开始的索引。

10

Public Overridable Function IndexOfValue (value As Object ) As Integer

返回SortedList中第一次出现的指定值的从零开始的索引。

11

Public Overridable Sub Remove (key As Object)

从SortedList中删除具有指定键的元素。

12

Public Overridable Sub RemoveAt (index As Integer)

删除SortedList的指定索引处的元素。

13

Public Overridable Sub TrimToSize

将容量设置为SortedList中的实际元素数。

例子 (Example)

以下示例演示了该概念 -

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
↑回到顶部↑
WIKI教程 @2018