ASP中有dictionary對象,類似雜湊表的索引值對,這樣的索引值對可以用在很多地方,可惜的是dictionary本身有很多不足之處,當我發現我無法將它使用的靈活時,我想到不如用數組自訂一個hash結構,記得以前曾寫過一個arraylist的asp封裝,於是有了本文的HashTable。
- <%
- Class HashTable
- '// Code By Shaoyun
- '// Date:2009-7-9 11:46:14
- '// Site:Devjs.com
- Private m_keys,m_vals,m_cnt,m_repeat
-
- Private Sub Class_Initialize()
- m_keys=array()
- m_vals=array()
- m_cnt=0
- m_repeat=false
- End Sub
-
- Private Sub Class_Terminate()
- End Sub
-
- ' 是否允許鍵名重複
- Public Property Let Repeat(boolval)
- m_repeat=boolval
- End Property
-
- ' 擷取索引值對的總數
- Public Property Get Count()
- Count=m_cnt
- End Property
-
- ' 擷取索引值
- Public Default Property Get Keys(keyname)
- dim i
- for i=0 to m_cnt-1
- if keyname=m_keys(i) then Keys=m_vals(i)
- next
- End Property
-
- ' 添加索引值
- Public Sub Add(key,val)
- dim done
- done=false
- If m_repeat Then
- done=true
- else
- if Not Exist(key) then done=true
- End If
- If done Then
- Redim Preserve m_keys(m_cnt)
- Redim Preserve m_vals(m_cnt)
- m_keys(m_cnt)=key
- m_vals(m_cnt)=val
- m_cnt=m_cnt+1
- end if
- End Sub
-
- ' 檢測鍵是否存在
- Public Function Exist(keyname)
- Exist=false
- dim i
- for i=0 to m_cnt-1
- if keyname=m_keys(i) then
- Exist=true
- Exit For
- end if
- next
- End Function
-
- ' 匯出資料
- Public function Dump()
- dim i,ostr
- for i= 0 to m_cnt-1
- ostr=ostr & "Array(""" & m_keys(i) & """," & m_vals(i) & "),"
- next
- if m_cnt>0 then ostr=mid(ostr,1,len(ostr)-1)
- Dump = "Array(" & ostr & ")"
- End function
-
- ' 通過數組賦值
- ' 數組傳值約定:array(array("item1",1),array("item2",2))
- Public Sub AssignByArray(ary)
- dim i
- for i=0 to ubound(ary)
- if IsArray(ary(i)) Then
- Add ary(i)(0),ary(i)(1)
- end if
- next
- End Sub
- End Class
-
- ' 使用執行個體
- set ht=new HashTable
- ht.add "haha",2
- ht.add "wawa",4
- ht.add "xixi",3
- ht.add "xixi",6
- ht.AssignByArray(array(array("a",1),array("b",2)))
- Response.Write ht.dump
- Response.Write ht.count
- Response.Write ht("haha")
- set ht=nothing
- %>
將以前寫的arraylist連結貼在後面,都是為了用著方便,沒什麼難度。
ASP自訂ArrayList類
連結地址:http://www.devjs.com/Article/25.aspx