asp中对数组的操作功能太弱,所以写了一个ArrayList类(参考c#中的ArrayList类),可以满足对数组操作的大部分功能.
对数组操作要注意的一些问题:
1,LBound()和UBound()是取得数组里最小的和最大的索引值,并不是数组的长度,
数组的长度是:UBound()+1
2,有时要动态改变数组大小,所以要先声明一个空的数组:
Dim arr()
使用这种方式声明的空数组如果用LBound()和UBound()取值的话会出现错误,但可以使用For Each:
Dim arr()
For Each v In arr
Response.Write v
Next
这样就不会出错了
另一种声明空数组的方法是:
Dim arr
arr=array()
这样的话使用LBound(arr)和UBound(arr)则分别返回0和-1.
为了避免发生错误,我推荐使用第二种方法声明空数组的
程序代码
<%
'/******************************
'类名:ArrayList
'名称:数组操作类
'日期:2007-11-6
'作者:西楼冷月
'网址:www.xilou.net | www.chinaCMS.org
'描述:对数组的各种操作
'版权:转载请注名出处,作者
'******************************
'***************ArrayList属性***************
'ArrayList.Length:数组长度
'ArrayList.Item:取得ArrayList某个索引的值,相当于ArrayList.GetValue()方法
'***************ArrayList方法***************
'---数组添加:
'ArrayList.Add(v):在ArrayList尾部添加一个元素,返回该元素所在的索引值
'ArrayList.AddArray(arr):在ArrayList尾部附加一个数组
'ArrayList.Insert(index,v):在ArrayList的index处插入一个值,原先的index和之后的值都往后移
'ArrayList.InsertArray(index,arr):在ArrayList的index处插入一个数组,原先的index和之后的值都往后移
'---数组更新:
'ArrayList.Update(index,v):更新索引为index处的值
'---数组删除:
'ArrayList.Remove(v):从ArrayList中删除第一个匹配项,注意是第一个,将会得到一新的数组
'ArrayList.RemoveAt(index):移除ArrayList的指定索引处的元素,将会得到一新的数组
'ArrayList.Splice(m,n):从一个数组中移除从索引m到索引n的一段元素,并返回这段移除的数组
'ArrayList.Clear()清空数组,数组将变为空,长度Length=0
'---数组查找:
'ArrayList.IndexOf(v):查找,返回ArrayList第一个匹配项的索引。没找到返回-1。
'ArrayList.LastIndexOf(v):返回ArrayList的最后一个匹配项的索引。没找到返回-1。
'---返回数组中的值:
'ArrayList.GetValue(index)取得ArrayList某个索引的值
'ArrayList.Slice(m,n)返回ArrayList从m到n的一段数组
'ArrayList.GetArray()返回整个Array数组
'---数组其他操作:
'ArrayList.Reverse()将整个 ArrayList 中元素的顺序反转
'ArrayList.Implode(separator)返回字符串值,元素由指定的分隔符分隔开来
'******************************/
Class ArrayList
Private arrList'//内部数组
Private arrLength'//记录数组的长度
Private Sub Class_Initialize()
arrList=Array()
arrLength=0
End Sub
Private Sub Class_Terminate()
Erase arrList
End Sub
'//数组长度,只读
Public Property Get Length
Length=arrLength
End Property
'//获取或设置指定索引处的元素
Public Default Property Get Item(index)
If Not isNum(index) Then
showErr "ArrayList.Item(index),非法的参数index":Exit Property
End If
If index < 0 or index > arrLength-1 Then
showErr "ArrayList.Item(index),index下标越界":Exit Property
End If
setValue Item,arrList(index)
End Property
Public Property Let Item(index,v)
If Not isNum(index) Then
showErr "ArrayList.Item(index,v),非法的参数index":Exit Property
End If
If index < 0 or index > arrLength-1 Then
showErr "ArrayList.Item(index,v),index下标越界":Exit Property
End If
setValue arrList(index),v
End Property
'//取得某个索引的值
Public Function GetValue(index)
If Not isNum(index) Then
showErr "ArrayList.GetValue(index),非法的参数index":Exit Function
End If
On Error Resume Next
setValue GetValue,arrList(index)
If Err Then showErr "ArrayList.GetValue(index),index"&Err.Description:Err.Clear:Exit Function
End Function
'//返回整个Array数组
Public Function GetArray()
GetArray=arrList
End Function
'//添加元素,将值添加到ArrayList的结尾处,返回当前元素的索引值
Public Function Add(v)
ReDim Preserve arrList(arrLength)
setValue arrList(arrLength),v
Add=arrLength
arrLength=arrLength+1
End Function
'//将数组添加到ArrayList的结尾处
Public Sub AddArray(arr)
If Not IsArray(arr) Then showErr "ArrayList.AddArray(arr),arr参数不是数组:":Exit Sub
Dim I,L,J
On Error Resume Next
If arrLength = 0 Then '//如果ArrayList为空则直接附值
arrList=arr
arrLength=arrLength+UBound(arr)+1
Else
L=arrLength+UBound(arr)'//新的数组长度
J=0
ReDim Preserve arrList(L)
For I = arrLength To L
setValue arrList(I),arr(J)
J=J+1
Next
arrLength=arrLength+UBound(arr)+1
End If
If Err Then showErr "ArrayList.AddArray(arr)"&Err.Description:Err.Clear:Exit Sub
End Sub
'//将元素插入ArrayList的指定index索引处,原有的arrList(index)及后面的元素都往后排
Public Sub Insert(index,v)
If Not isNum(index) Then
showErr "ArrayList.Insert(index,v),非法的参数index":Exit Sub
End If
Dim I,v2
If index<arrLength And index>=0 Then
ReDim Preserve arrList(arrLength)
arrLength=arrLength+1
For I = index To arrLength - 1
setValue v2,arrList(I)'//交换值
setValue arrList(I),v
setValue v,v2
Next
Else
showErr "ArrayList.Insert(index,v),index下标越界"
End If
End Sub
'//将一组数组插入到指定的index处
Public Sub InsertArray(index,arr)
If Not isNum(index) Then
showErr "ArrayList.InsertArray(index,arr),非法的参数index":Exit Sub
End If
If index < 0 or index > arrLength-1 Then
showErr "ArrayList.InsertArray(index,arr),index下标越界":Exit Sub
End If
If Not IsArray(arr) Then showErr "ArrayList.InsertArray(index,arr),arr参数不是数组":Exit Sub
Dim I,L1,L2,J:J=0
On Error Resume Next
L1=UBound(arr)
L2=arrLength+L1
ReDim Preserve arrList(L2)
For I = arrLength -1 To index Step -1
setValue arrList(I+L1+1),arrList(I)'//把index之后的值往后移
Next
For I = index To index+L1
setValue arrList(I),arr(J)
J=J+1
Next
If Err Then showErr "ArrayList.InsertArray(index,arr),"&Err.Description:Err.Clear:Exit Sub
arrLength=arrLength+L1+1'//新的数组长度
End Sub
'//更新数组中索引为index的对应值
'//by xilou 39949376
Public Sub Update(index,v)
If Not isNum(index) Then
showErr "ArrayList.Update(index,v),非法的参数index:":Exit Sub
End If
If index < 0 or index > arrLength-1 Then
showErr "ArrayList.Update(index,v),index下标越界":Exit Sub
End If
setValue arrList(index),v
End Sub
'//从ArrayList中删除第一个匹配项,注意是第一个,将会得到一新的数组
Public Sub Remove(v)
Dim I,index
index = -1 '//第一个匹配的索引
For I = 0 To arrLength - 1
If arrList(I)=v Then index = I : Exit For
Next
If index <> -1 Then
For I = index To arrLength - 2
setValue arrList(I),arrList(I+1)'//值向前填充
Next
ReDim Preserve arrList(arrLength-1)'//收缩数组
arrLength = arrLength - 1
End If
End Sub
'//移除ArrayList的指定索引处的元素,将会得到一新的数组
Public Sub RemoveAt(index)
If Not isNum(index) Then
showErr "ArrayList.RemoveAt(index),非法的参数index":Exit Sub
End If
If index < 0 or index > arrLength-1 Then
showErr "ArrayList.RemoveAt(index,v),index下标越界":Exit Sub
End If
If index > 0 Then
For I = index To arrLength - 2
setValue arrList(I),arrList(I+1)'//值向前填充
Next
ReDim Preserve arrList(arrLength-1)'//收缩数组
arrLength = arrLength - 1
End If
End Sub
'//从一个数组中移除从索引m到索引n的一段元素,并返回这段移除的数组
Public Function Splice(m,n)
If Not isNum(m) Then
showErr "ArrayList.Splice(m,n),非法的参数m":Exit Function
End If
If Not isNum(n) Then
showErr "ArrayList.Splice(m,n),非法的参数n":Exit Function
End If
If m < 0 or m > arrLength - 1 Then
showErr "ArrayList.Splice(),m下标越界":Exit Function
End If
If n < 0 or n > arrLength - 1 Then
showErr "ArrayList.Splice(),n下标越界":Exit Function
End If
Dim newArr,x,L,I,J
newArr=Array()
If m > n Then x=m:m=n:n=x '//交换数值
L=n-m
ReDim Preserve newArr(L)
For I = m To n
setValue newArr(J),arrList(I)'要移除的元素
J=J+1
Next
'//把n后面的元素的值移前
For I = (n+1) To arrLength -1
setValue arrList(I-L-1),arrList(I)
Next
arrLength=arrLength-L-1
ReDim Preserve arrList(arrLength)
Splice=newArr
End Function
'//清空数组,数组将变为空,长度Length=0
Public Sub Clear()
Erase arrList
arrLength=0
End Sub
'//将整个 ArrayList 中元素的顺序反转
Public Sub Reverse()
Dim L,I,J,v
J=arrLength-1
If arrLength > 0 Then
L=Int(arrLength/2)
For I = 0 To L-1
setValue v,arrList(I)
setValue arrList(I),arrList(J)
setValue arrList(J),v
J=J-1
Next
End If
End Sub
'//返回字符串值,其中包含了连接到一起的数组的所有元素,元素由指定的分隔符分隔开来
Public Function Implode(separator)
On Error Resume Next
Implode=Join(arrList,separator)
If Err Then showErr "ArrayList.Implode(separator),"&Err.Description:Err.Clear:Exit Function
End Function
'//返回ArrayList从m到n的一段数组
Public Function Slice(m,n)
If Not isNum(m) Then
showErr "ArrayList.Slice(m,n),非法的参数m":Exit Function
End If
If Not isNum(n) Then
showErr "ArrayList.Slice(m,n),非法的参数n":Exit Function
End If
If m < 0 or m > arrLength-1 Then
showErr "ArrayList.Slice(m,n),m下标越界":Exit Function
End If
If n < 0 or n > arrLength-1 Then
showErr "ArrayList.Slice(m,n),n下标越界":Exit Function
End If
Dim I,J,newArr()
J=0
If m<=n Then
ReDim Preserve newArr(n-m)
For I = m To n
setValue newArr(J),arrList(I)
J=J+1
Next
Else
ReDim Preserve newArr(m-n)
For I = n To m
setValue newArr(J),arrList(I)
J=J+1
Next
End If
Slice=newArr
Erase newArr
End Function
'//查找,返回ArrayList第一个匹配项的从零开始的索引。没找到返回-1。
'//by xilou 39949376
Public Function IndexOf(v)
Dim I
For I = 0 To arrLength - 1
If arrList(I)=v Then IndexOf=I:Exit Function
Next
IndexOf=-1
End Function
'//返回ArrayList的最后一个匹配项的从零开始的索引。没找到返回-1。
Public Function LastIndexOf(v)
Dim I
If arrLength=0 Then
LastIndexOf=-1:Exit Function
Else
For I = (arrLength-1) To 0 Step -1
If arrList(I)=v Then LastIndexOf=I:Exit Function
Next
End If
LastIndexOf=-1
End Function
'//将v2赋值给v1,设置不同的值(对象或普通值)
Private Sub setValue(byref v1,v2)
If IsObject(v2) Then
Set v1=v2
Else
v1=v2
End If
End Sub
'//判断是否是数字,返回true or false
Private Function isNum(v)
If Not IsNumeric(v) or IsEmpty(v) Then
isNum=False
Else
isNum=True
End If
End Function
'//显示错误
Private Sub showErr(errInfo)
Response.Write "<div id=""ERRORINFO"" style=""font-size:12px;color:#990000;font-family:""新宋体"", Arial"">"
Response.Write errInfo
Response.Write "</div>"
'Response.End()
End Sub
End Class
%>