asp模拟javascript数组对象ArrayList类

除了一个join方法(使用JoinBy代替)其它名称上基本和js相同,另外又添加了几个有用的方法

实际应用的效率尚未测试.

类代码(ArrayList.asp):

<%
'******************************
'类名:ArrayList
'日期:2012-1-9
'作者:shirne
'网址:http://www.shirne.com
'描述:模拟javascript中数组对象的
'		一些功能和其它相关功能
'版权:可在程序中自由使用但请注明出处,作者
'******************************
Class ArrayList
	
	Private arrList()		'内部数组
	
	Private Sub Class_Initialize()
		'ReDim arrList(0)
	End Sub
	
	Private Sub Class_Terminate()
		Erase arrList
	End Sub
	
	'数组长度,读取
	Public Property Get Length
		On Error Resume Next
		Length=UBound(arrList)+1
		If Err Then Err.Clear:Length=0
		On Error GoTo 0
	End Property
	
	'数组长度,设定,小于1将清空数组
	Public Property Let Length( L)
		If L<1 Then
			Erase arrList
		Else
			ReDim Preserve arrList(L-1)
		End If
	End Property
	
	'取得某个索引的值,索引小于0将倒序取,大于长度返回Empty
	Public Default Property Get Value(idx)
		Dim L
		L=Length
		If L<1 Then
			Value=Empty
		ElseIf idx<0 Then
			idx=L-(Abs(idx) Mod L)
			Value=arrList(idx)
		ElseIf idx>=L Then
			Value=Empty
		Else
			Value=arrList(idx)
		End If
	End Property
	
	Public Property Let Value(idx, val)
		If idx>=Length Then
			ReDim Preserve arrList(idx)
		End If
		arrList(idx)=val
	End Property
	
	'返回整个Array数组
	Public Property Get Array()
		Array=arrList
	End Property
	
	'添加元素
	Public Sub Push(v)
		Dim L
		L=Length
		ReDim Preserve arrList(L)
		arrList(L)=v
	End Sub
	
	'将元素插入ArrayList的指定索引处,原有位置及后面的元素都往后排,如果索引超出,将插入到最后
	Public Sub Insert(idx,v)
		Dim I, v2, L
		L=Length
		If idx<L And idx>=0 Then
			ReDim Preserve arrList(L)
			For I = idx To L
				v2=arrList(I)
				arrList(I)=v
				v=v2
			Next
		Else
			Value(L)=v
		End If
	End Sub
	
	'连接一个新的数组到最后
	Public Sub Concat(arr)
		If Not IsArray(arr) Then showErr "参数不是数组(arr):ArrayList.Concat()":Exit Sub
		Dim I, nL, J, L
		L=Length
		If L = 0 Then
			arrList=arr
		Else
			nL=L+UBound(arr)
			J=0
			ReDim Preserve arrList(nL)
			For I = L To nL
				arrList(I)=arr(J)
				J=J+1
			Next
		End If
	End Sub
	
	'将一组数组插入到指定的索引处,索引大于数组长度,将插入到最后
	Public Sub InsertArray(idx,arr)
		If idx = "" Or Not IsNumeric(idx) Then 
			showErr "非法的参数:ArrayList.InsertArray()":Exit Sub
		End If
		If Not IsArray(arr) Then
			showErr "参数不是数组:ArrayList.InsertArray()":Exit Sub
		End If
		
		Dim I, L1, L2, J, L
		L=Length
		J=0
		If L<1 Or idx>L Then
			Concat arr
		Else
			L1=UBound(arr)
			L2=L+L1
			ReDim Preserve arrList(L2)
			For I = l -1 To idx Step -1
				arrList(I+L1+1)=arrList(I)
			Next
			For I = idx To idx+L1
				arrList(I)=arr(J)
				J=J+1
			Next
		End If
	End Sub
	
	'从ArrayList中删除第一个匹配项
	Public Sub Remove(v)
		Dim I,idx, L
		L=Length-1
		idx = -1
		For I = 0 To L
			If arrList(I)=v Then idx = I : Exit For
		Next
		If idx <> -1 Then
			For I = idx To L-1
				arrList(I) = arrList(I+1)
			Next
			ReDim Preserve arrList(L-1)
		End If
	End Sub
	
	'移除ArrayList的指定索引处的元素,并返回该元素
	Public Function RemoveAt(idx)
		If idx <> "" And IsNumeric(idx) Then 
			Dim L
			L=Length-1
			If idx >= 0 And idx<=L Then
				RemoveAt=arrList(idx)
				For I = idx To L-1
					arrList(I) = arrList(I+1)
				Next
				ReDim Preserve arrList(L-1)
			End If
		End If
	End Function
	
	'从一个数组中移除从索引m到索引n的一段元素,并返回这段移除的数组
	Public Function Splice(m,n)
		If m = "" Or n = "" Or Not IsNumeric(m) Or Not IsNumeric(n) Then 
			showErr "非法的参数(m,n):ArrayList.Splice()":Exit Function
		End If
		Dim newArr(), x, L, I, J, B
		L=Length-1
		If m > n Then x=m:m=n:n=x
		m=Min(L-1,m)
		n=Min(L-1,n)
		B=n-m
		J=0
		ReDim newArr(B)
		For I = m To n
			newArr(J)=arrList(I)
			J=J+1
		Next
		
		For I = m To L-B-1
			arrList(I)=arrList(I+B+1)
		Next
		ReDim Preserve arrList(L-B-1)
		Splice=newArr
	End Function
	
	'清空数组,数组将变为空,效果与设定Length小于1相同
	Public Sub Clear()
		Erase arrList
	End Sub
	
	'反转数组
	Public Sub Reverse()
		Dim L, I, J, v, H
		L=Length
		J=L-1
		If L > 0 Then
			H=Int(L/2)
			For I = 0 To H-1
				v=arrList(I)
				arrList(I)=arrList(J)
				arrList(J)=v
				J=J-1
			Next
		End If
	End Sub
	
	'使用指定字符连接数组的值
	Public Function JoinBy(separator)
		JoinBy=Join(arrList,separator)
	End Function
	
	'返回ArrayList从m到n的一段数组
	Public Function Slice(m,n)
		If m = "" Or n = "" Or Not IsNumeric(m) Or Not IsNumeric(n) Then 
			showErr "非法的参数:ArrayList.Slice()":Exit Function
		End If
		
		Dim I, J, K, newArr(), L
		L=Length
		J=0
		If L<1 Then Slice=Split("",""):Exit Function
		If m < 0 Then m=0
		If n < 0 Then n=0
		K=Min(Max(m,n),L-1)
		ReDim newArr(Abs(m-n))
		For I=Min(m, n) To K
			newArr(J)=arrList(I)
			J=J+1
		Next
		Slice=newArr
		Erase newArr
	End Function
	
	'查找值,返回ArrayList第一个匹配项的从零开始的索引。没找到返回-1。
	Public Function IndexOf(v)
		Dim I
		For I = 0 To Length - 1
			If arrList(I)=v Then IndexOf=I:Exit Function
		Next
		IndexOf=-1
	End Function
	
	'反向查找,返回ArrayList的最后一个匹配项的从零开始的索引。没找到返回-1。
	Public Function LastIndexOf(v)
		Dim I, L
		L=Length
		LastIndexOf=-1
		If L>0 Then
			For I = (L-1) To 0 Step -1
				If arrList(I)=v Then LastIndexOf=I:Exit Function
			Next
		End If
	End Function
	
	'显示错误
	Private Sub showErr(errInfo)
		Response.Write errInfo
		Response.Write "<br />"
		If Err Then
			Response.Write Err.Number
			Response.Write ":"
			Response.Write Err.Description
			Response.Write "<br />"
		End If
		Response.End()
	End Sub
End Class
%>

 

演示代码(ArrayListTest.asp):

 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="ArrayList.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>数据操作类测试</title>
</head>

<body>
<%
Dim arr
Set arr=New ArrayList
Response.Write "---创建新数组<br>"
Response.Write "arr.length:"&arr.length &"<br>"

Response.Write "<br>---添加元素,直接赋值,push方法<br>"
arr(0)="第一个元素"
arr(1)="A"
arr(2)="B"
arr(4)="C"
arr(6)="D"
arr(7)="E"
arr(8)="第九个元素"
arr.push("第十个元素")
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(0):"&arr(0) &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---连接数组<br>"
arr.Concat(Array("a","b","c","d","e"))
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---remove<br>"
arr.Remove("a")
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---removeat<br>"
arr.RemoveAt(10)
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---Insert<br>"
arr.Insert 10,"xxx"
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---InsertArray<br>"
arr.InsertArray 10,Array("yyy","zzz")
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---Reverse<br>"
arr.Reverse()
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---IndexOf<br>"
Response.Write "IndexOf(""xxx""):"&arr.IndexOf("xxx") &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---LastIndexOf<br>"
Response.Write "LastIndexOf(""xxx""):"&arr.IndexOf("xxx") &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---Slice(8,12)<br>"
newarr=arr.Slice(8,12)
Response.Write "Slice Array:"& Join(newarr,", ") &"<br>"
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Response.Write "<br>---Splice(8,12)<br>"
newarr=arr.Splice(8,12)
Response.Write "splice Array:"& Join(newarr,", ") &"<br>"
Response.Write "arr.length:"&arr.length &"<br>"
Response.Write "arr(12):"&arr(12) &"<br>"
Response.Write "arr.JoinBy:"&arr.JoinBy(", ") &"<br>"

Function Max(a, b)
	If a>b Then
		Max=a
	Else
		Max=b
	End If
End Function
Function Min(a, b)
	If a<b Then
		Min=a
	Else
		Min=b
	End If
End Function
%>
</body>
</html>