java中的hashMap存取數據非常方便,可惜ASP中沒有類似的類。作者在開發程序中需要類似的數據類型,於是構造了一個能基本類似hashMap功能的類,可以實現鍵值存取操作等,存取的數據可以為ASP 中的任何基本類型。
下面是程序的代碼,貼到一個空的ASP中可以直接運行。
<% set jb51 = new Jb '給mp對象賦值 jb51.putv "a","jb51.net" jb51.putv "b","www.jb51.net" jb51.putv "c","http://www.jb51.net" response.write "[鍵值數量]:"&jb51.count response.write "<br>" response.write "[a]:"&jb51.getv("a") response.write "<br>" response.write ":"&jb51.getv("b") response.write "<br>" response.write "[c]:"&jb51.getv("c") response.write "<hr>" '刪除key為"b"的鍵值 jb51.delv "b" response.write "[鍵值數量]:"&jb51.count response.write "<br>" response.write "[a]:"&jb51.getv("a") response.write "<br>" response.write ":"&jb51.getv("b") response.write "<br>" response.write "[c]:"&jb51.getv("c") response.write "<hr>" '清空jb51的所有值 jb51.clear '給key為"c"的鍵值重新賦值 jb51.putv "c","http://www.jb51.net" response.write "[鍵值數量]:"&jb51.count response.write "<br>" response.write "[a]:"&jb51.getv("a") response.write "<br>" response.write ":"&jb51.getv("b") response.write "<br>" response.write "[c]:"&jb51.getv("c") response.write "<hr>" Class MtMap private arr() private arr_len '構造函數 private Sub Class_Initialize '其中 arr(0,n)為key,arr(1,n)為value arr_len = 0 redim arr(1,arr_len) end sub '賦值,如果存在則覆蓋 public sub putv(k,v) dim is_update is_update = false arr_len = ubound(arr,2) for i=0 to arr_len-1 if k=arr(0,i) then arr(1,i) = v is_update = true exit for end if next if not is_update then arr_len = arr_len +1 redim preserve arr(1,arr_len) arr(0,arr_len) = k arr(1,arr_len) = v end if end sub '取得key為"k"的鍵值 public function getv(k) dim v v = "" for i=0 to arr_len if k=arr(0,i) then v = arr(1,i) exit for end if next getv = v end function '刪除key為"k"的鍵值 public sub delv(k) arr_len = ubound(arr,2) for i=0 to arr_len if k=arr(0,i) then v = arr(1,i) for k = i to arr_len-1 arr(0,k) = arr(0,k+1) arr(1,k) = arr(1,k+1) next arr_len = arr_len - 1 redim preserve arr(1,arr_len) exit for end if next end sub '獲得jb51中鍵值的數量 public property get count() count = arr_len end property '清空jb51中所有的鍵值 public sub clear() arr_len = 0 redim arr(1,1) end sub end class %>
以上就是分享的ASP如何實現類似hashMap功能的類,希望對大家的學習有所啟發。