|
ASP应用程序中使用System.Collections.Sortedlist 可按照键和索引访问,自动排序。
SortedList 类表示键/值对的集合,这些键值对按键排序并可按照键和索引访问。
主要成员:
/* 属性 */
Capacity; //容量
Count; //元素数
Keys; //键集合(ICollection)
Values; //值集合(ICollection)
/* 方法 */
Add() //添加
Clear() //清空
Contains() //是否包含指定键
ContainsKey() //同 Contains()
ContainsValue() //是否包含指定值
GetByIndex() //根据索引取 Value
GetKey() //根据索引获取 Key
GetKeyList() //取键列表(IList)
GetValueList() //取值列表(IList)
IndexOfKey() //获取指定键的索引
IndexOfValue() //获取指定值的索引
Remove() //根据键值删除
RemoveAt() //根据索引删除
SetByIndex() //根据索引设置值
TrimToSize() //优化容量(Capacity = Count)
- <%
- response.Charset="utf-8"
- set list = server.createObject("System.Collections.Sortedlist")
- with list
- .add "Second", "是"
- .add "Third", "美丽的日子"
- .add "First", "今天"
- end with
- for i = 0 to list.count - 1
- response.write(list.getKey(i) & " = " & list.getByIndex(i))&"<br>"
- next
- set list = nothing
- %>
复制代码
- response.Charset="utf-8"
- Dim objDict
- Set objDict = Server.CreateObject("System.Collections.Sortedlist")
- objDict.Add "张三", "80分"
- objDict.Add "李四", "60分"
- objDict.Add "王五", "90分"
- objDict.Add "赵六", "20分"
- response.Write "王五分数是:" & vbTab & objDict.GetByIndex(objDict.IndexOfKey("王五"))
- Set objDict = Nothing
复制代码
- response.Charset="utf-8"
- Dim objDict
- Dim objSortedList
- Dim objList2
- Dim I
- Set objSortedList = Server.CreateObject("System.Collections.Sortedlist")
- objSortedList.Add "First", "AAAA"
- objSortedList.Add "Second", "!"
- objSortedList.Add "Third", "CCCC"
- objSortedList.Add "Fourth", ","
- Response.Write objSortedList.IndexOfKey("First")&vbnewline
- Response.Write objSortedList.IndexOfValue("AAAA")&vbnewline
- Response.Write objSortedList.IndexOfKey("Second")&vbnewline
- Response.Write objSortedList.IndexOfValue("!")&vbnewline
- Response.Write objSortedList.IndexOfKey("Third")&vbnewline
- Response.Write objSortedList.IndexOfValue("CCCC")&vbnewline
- Response.Write objSortedList.IndexOfKey("Fourth")&vbnewline
- Response.Write objSortedList.IndexOfValue(",")&vbnewline
- For I = 0 To objSortedList.Count - 1
- Response.Write objSortedList.GetKey(I) & vbTab & objSortedList.GetByIndex(I)&vbnewline
- Next
- Response.Write "Size : " & objSortedList.Count&vbnewline
- Response.Write "Capacity : " & objSortedList.Capacity&vbnewline
- Response.Write objSortedList.GetByIndex(objSortedList.IndexOfKey("Third"))&vbnewline
- objSortedList.TrimToSize
- Response.Write "Size : " & objSortedList.Count&vbnewline
- Response.Write "Capacity : " & objSortedList.Capacity&vbnewline
- Set objList2 = objSortedList.Clone
- Response.Write "Sorted List Key(1) = " & objSortedList.GetKey(1)&vbnewline
- Response.Write "Cloned List Key(1) = " & objList2.GetKey(1)
- Set objList2 = Nothing
- Set objSortedList = Nothing
复制代码 |
|