ASP版ArrayList类,请高手指正。

netbooting 2011-11-28 09:06:29

<%
'**********************************************
'类名:ArrayList
'作者:lsh825@126.com
'日期:2011年11月26日
'说明:基于西楼冷月ArrayList类修改
'**********************************************

'******************************
'ArrayList 属性
'******************************
'Length 数组长度

'******************************
'ArrayList 方法
'******************************
'//添加:
'Add(v) 在ArrayList尾部添加一个元素
'AddArray(arr) 在ArrayList尾部附加一个数组
'Insert(index, v) 在ArrayList的index处插入一个值,之后的值都往后移
'InsertArray(index, arr) 在ArrayList的index处插入一个数组,之后的值都往后移

'//更新:
'UpdateIndex(index, v) 更新索引为index处的值
'UpdateValue(m, n, v) 将ArrayList中m至n间的元素值替换为v
'ReplaceValue(v, newValue) 将ArrayList中所有值为v的元素替换为newValue

'//删除:
'RemoveAt(index) 移除指定索引处的元素
'Remove(v, mode) 删除由mode指定的元素
' mode = 1 删除第一个匹配项
' mode = n 删除第n个匹配项
' mode = -1 删除最后一个匹配项
' mode = 0 删除所有匹配项
'SubArray(m, n) 返回从索引m到索引n的子数组
'SubArrayLength(m, length) 返回从索引m开始,包含length个元素的数组
'Cut(m, n) 删除从索引m到索引n的元素,得到一个新数组
'CutLength(m, length) 删除从索引m开始的length个元素,得到一个新数组
'Clear() 清空数组,长度Length = 0
'RemoveRepeat() 删除数组中的重复项,只保留一项。

'//查找:
'IndexOf(v) 返回ArrayList第一个匹配项的索引,没找到返回-1
'LastIndexOf(v) 返回ArrayList的最后一个匹配项的索引,没找到返回-1
'GetValue(index) 取得ArrayList某个索引的值,若为负数,则从尾部开始算起
'IsInclude(v) 若ArrayList中任一元素等于v,则返回True
'MaxValue() 返回ArrayList中的最大值
'MinValue() 返回ArrayList中的最小值

'//其他:
'Reverse() 将整个ArrayList中的元素反序排列
'Implode(separator) 返回字符串值,元素由指定的分隔符分隔开来
'GetArray() 返回整个Array数组

'//排序:
'SortAsc() 将数组的值按升序排列
'SortDesc() 将数组的值按降序排列
'SortRnd() 将数组的元素随机排列

'//数组比较
'IsArrayEqual(arr1, arr2) 比较两个数组是否相等,若所有元素都相等则返回True
'MathIntersection(arr1, arr2) 求两个数组的交集
'MathUnion(arr1, arr2) 求两个数组的并集
'
%>
...全文
159 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
p2227 2011-12-03
  • 打赏
  • 举报
回复
发成资源吧
netbooting 2011-12-03
  • 打赏
  • 举报
回复



<%
'**********************************************
'代码段三
'**********************************************
'******************************************************************************************
'//其他:
'Reverse() 将整个ArrayList中的元素反序排列
'Implode(separator) 返回字符串值,元素由指定的分隔符分隔开来
'GetArray() 返回整个Array数组
'******************************************************************************************
'将整个ArrayList中的元素反序排列
Public Sub Reverse()
Dim length, i, j, v
j = arrLength - 1
If arrLength > 0 Then
length = Int(arrLength / 2)
For i = 0 To length - 1
v = arrList(i)
arrList(i) = arrList(j)
arrList(j) = v
j = j - 1
Next
End If
End Sub

'返回字符串值,元素由指定的分隔符分隔开来
Public Function Implode(separator)
Implode = Join(arrList, separator)
End Function

'返回整个Array数组
Public Function GetArray()
GetArray = arrList
End Function

'******************************************************************************************
'//排序:
'SortAsc() 将数组的值按升序排列
'SortDesc() 将数组的值按降序排列
'SortRnd() 将数组的元素随机排列
'******************************************************************************************
'将数组的值按升序排列
Public Sub SortAsc()
Dim i, j, temp, changed
changed = True

i = 0
Do Until Not changed
'如果一轮循环没有数据交换,则说明已排好序。
changed = False
For j = 0 To UBound(arrList) - (i + 1)
'最大值下沉
If arrList(j) > arrList(j + 1) Then
temp = arrList(j)
arrList(j) = arrList(j + 1)
arrList(j + 1) = temp
changed = True
End If
Next
i = i + 1
Loop
End Sub

'将数组的值按降序排列
Public Sub SortDesc()
Dim i, j, temp, changed
changed = True

i = 0
Do Until Not changed
'如果一轮循环没有数据交换,则说明已排好序。
changed = False
For j = 0 To UBound(arrList) - (i + 1)
'最大值下沉
If arrList(j) < arrList(j + 1) Then
temp = arrList(j)
arrList(j) = arrList(j + 1)
arrList(j + 1) = temp
changed = True
End If
Next
i = i + 1
Loop
End Sub

'将数组的元素随机排列
Public Sub SortRnd()
Dim temp, index
Randomize
For i = 0 To arrLength - 1
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
index = Int((UBound(arrList) - 0 + 1) * Rnd() + 0)
temp = arrList(index)
arrList(index) = arrList(i)
arrList(i) = temp
Next
End Sub


'******************************************************************************************
'//数组比较
'IsArrayEqual(arr1, arr2) 比较两个数组是否相等,若所有元素都相等则返回True
'MathIntersection(arr1, arr2) 求两个数组的交集
'MathUnion(arr1, arr2) 求两个数组的并集
'******************************************************************************************
'比较两个数组是否相等,若所有元素都相等则返回True
Public Function IsArrayEqual(arr1, arr2)
Dim isEqual
isEqual = False
If UBound(arr1) = UBound(arr2) Then
isEqual = True
For i = 0 To UBound(arr1)
If arr1(i) <> arr2(i) Then
isEqual = False
Exit For
End If
Next
End If
IsArrayEqual = isEqual
End Function

'求两个数组的交集
Public Function MathIntersection(arr1, arr2)
Dim i, dic, arr, dicIntersection
Set dic = Server.CreateObject("Scripting.Dictionary")
Set dicIntersection = Server.CreateObject("Scripting.Dictionary")
For i = 0 To UBound(arr1)
dic.Add arr1(i), 0
Next
For i = 0 To UBound(arr2)
If dic.Exists(arr2(i)) Then
dicIntersection.Add arr2(i), 0
End If
Next
arr = dicIntersection.Keys
Set dic = Nothing
Set dicIntersection = Nothing
MathIntersection = arr
End Function

'求两个数组的并集
Public Function MathUnion(arr1, arr2)
Dim i, dic, arr
Set dic = Server.CreateObject("Scripting.Dictionary")
For i = 0 To UBound(arr1)
If Not dic.Exists(arr1(i)) Then
dic.Add arr1(i), 0
End If
Next
For i = 0 To UBound(arr2)
If Not dic.Exists(arr2(i)) Then
dic.Add arr2(i), 0
End If
Next
arr = dic.Keys
Set dic = Nothing
MathUnion = arr
End Function

'显示错误
Private Sub showErr(errInfo)
Response.Write "<div id=""ERRORINFO"" style=""font-size:12px;color:#990000;font-family:Lucida Console,Verdana"">"
Response.Write errInfo
Response.Write "</div>"
Response.End()
End Sub
End Class
%>


hookee 2011-11-28
  • 打赏
  • 举报
回复
支持一下,
另外,ASP可以直接用.Net的ArrayList
挨踢直男 2011-11-28
  • 打赏
  • 举报
回复
看了一下接口
最好再提供一个遍历的接口
实现一个迭代器
netbooting 2011-11-28
  • 打赏
  • 举报
回复


<%
'**********************************************
'代码段二
'**********************************************

'******************************************************************************************
'//删除:
'RemoveAt(index) 移除指定索引处的元素
'Remove(v, mode) 删除由mode指定的元素
' mode = 1 删除第一个匹配项
' mode = n 删除第n个匹配项
' mode = -1 删除最后一个匹配项
' mode = 0 删除所有匹配项
'SubArray(m, n) 返回从索引m到索引n的子数组
'SubArrayLength(m, length) 返回从索引m开始,包含length个元素的数组
'Cut(m, n) 删除从索引m到索引n的元素,得到一个新数组
'CutLength(m, length) 删除从索引m开始的length个元素,得到一个新数组
'Clear() 清空数组,长度Length = 0
'RemoveRepeat() 删除数组中的重复项,只保留一项。
'******************************************************************************************
'移除指定索引处的元素
Public Sub RemoveAt(index)
If index = "" Or Not IsNumeric(index) Then
showErr "错误:非法的index参数【ArrayList.InsertArray()】"
Exit Sub
End If
If index < 0 Or index > arrLength - 1 Then
showErr "错误:index索引越界【ArrayList.InsertArray()】"
Exit Sub
End If
If index >= 0 Then
For i = index To UBound(arrList) - 1
arrList(i) = arrList(i + 1) '值向前填充
Next
arrLength = arrLength - 1
ReDim Preserve arrList(arrLength - 1) '收缩数组
End If
End Sub

'删除由mode指定的元素
Public Sub Remove(v, mode)
Dim i, index, count
index = -1 '匹配项的索引
Select Case mode
Case 1
For i = 0 To arrLength - 1
If arrList(i) = v Then
index = i
Exit For
End If
Next
If index <> -1 Then RemoveAt index
Case -1
For i = arrLength - 1 To 0 Step -1
If arrList(i) = v Then
index = i
Exit For
End If
Next
If index <> -1 Then RemoveAt index
Case 0
Dim tempArr
count = 0
tempArr = arrList
For i = 0 To arrLength - 1
If tempArr(i) = v Then
index = i - count
RemoveAt index
count = count + 1
End if
Next
Case Else
count = 0
If mode > 1 And mode <= arrLength Then
For i = 0 To arrLength - 1
If arrList(i) = v Then
count = count + 1
If count = mode Then
index = i
RemoveAt index
Exit For
End If
End If
Next
Else
showErr "错误:非法的mode参数【ArrayList.Remove()】"
Exit Sub
End if
End Select
End Sub

'返回从索引m到索引n的子数组
Public Sub SubArray(m, n)
If m = "" Or Not IsNumeric(m) Then
showErr "错误:非法的m参数【ArrayList.UpdateValue()】"
Exit Sub
End If
If n = "" Or Not IsNumeric(n) Then
showErr "错误:非法的n参数【ArrayList.UpdateValue()】"
Exit Sub
End If
If m < 0 Or m > arrLength - 1 Then
showErr "错误:m索引越界【ArrayList.UpdateIndex()】"
Exit Sub
End If
If n < 0 Or n > arrLength - 1 Then
showErr "错误:n索引越界【ArrayList.UpdateIndex()】"
Exit Sub
End If
If m > n Then
temp = m
m = n
n = temp
End If

Dim newArr, newLength, i, j
newArr = Array()
newLength = n - m + 1
ReDim Preserve newArr(newLength - 1)
For i = m To n
newArr(j) = arrList(i) '要移除的元素
j = j + 1
Next
arrList = newArr
arrLength = newLength
End Sub

'返回从索引m开始,包含length个元素的数组
Public Sub SubArrayLength(m, length)
Dim n
n = m + length - 1
SubArray m, n
End Sub

'删除从索引m到索引n的元素,得到一个新数组
Public Sub Cut(m, n)
If m = "" Or Not IsNumeric(m) Then
showErr "错误:非法的m参数【ArrayList.UpdateValue()】"
Exit Sub
End If
If n = "" Or Not IsNumeric(n) Then
showErr "错误:非法的n参数【ArrayList.UpdateValue()】"
Exit Sub
End If
If m < 0 Or m > arrLength - 1 Then
showErr "错误:m索引越界【ArrayList.UpdateIndex()】"
Exit Sub
End If
If n < 0 Or n > arrLength - 1 Then
showErr "错误:n索引越界【ArrayList.UpdateIndex()】"
Exit Sub
End If
If m > n Then
temp = m
m = n
n = temp
End If

Dim newLength, i, j
newLength = n - m
'把n后面的元素的值移前
For i = (n + 1) To arrLength -1
arrList(i - newLength - 1) = arrList(i)
Next
arrLength = arrLength - newLength - 1
ReDim Preserve arrList(arrLength - 1)
End Sub

'删除从索引m开始的length个元素,得到一个新数组
Public Sub CutLength(m, length)
Dim n
n = m + length - 1
Cut m, n
End Sub

'清空数组,长度Length = 0
Public Sub Clear()
Erase arrList
arrLength = 0
End Sub

'删除数组中的重复项,只保留一项。
Public Sub RemoveRepeat()
Dim i, dic
Set dic = Server.CreateObject("Scripting.Dictionary")
For i = 0 To UBound(arrList)
If Not dic.Exists(arrList(i)) Then
dic.Add arrList(i), 0
End If
Next
arrList = dic.Keys
arrLength = UBound(arrList) + 1
Set dic = Nothing
End Sub

'******************************************************************************************
'//查找:
'IndexOf(v) 返回ArrayList第一个匹配项的索引,没找到返回-1
'LastIndexOf(v) 返回ArrayList的最后一个匹配项的索引,没找到返回-1
'GetValue(index) 取得ArrayList某个索引的值,若为负数,则从尾部开始算起
'IsInclude(v) 若ArrayList中任一元素等于v,则返回True
'MaxValue() 返回ArrayList中的最大值
'MinValue() 返回ArrayList中的最小值
'******************************************************************************************
'返回ArrayList第一个匹配项的索引,没找到返回-1
Public Function IndexOf(v)
Dim i, index
index = -1
For i = 0 To arrLength - 1
If arrList(I) = v Then
index = i
Exit For
End If
Next
IndexOf = index
End Function

'返回ArrayList的最后一个匹配项的索引,没找到返回-1
Public Function LastIndexOf(v)
Dim i, index
index = -1
If arrLength <> 0 Then
For i = (arrLength - 1) To 0 Step -1
If arrList(i) = v Then
index = i
Exit For
End If
Next
End If
LastIndexOf = index
End Function

'取得ArrayList某个索引的值,若为负数,则从尾部开始算起
Public Function GetValue(index)
On Error Resume Next
If index = "" Or Not IsNumeric(index) Then
showErr "错误:非法的index参数【ArrayList.GetValue()】"
Exit Function
End If
If index > arrLength - 1 Then
showErr "错误:index索引越界【ArrayList.GetValue()】"
Exit Function
End If
If index < 0 And Abs(index) > arrLength Then
showErr "错误:负数index索引越界【ArrayList.GetValue()】"
Exit Function
End If

Dim value
If index >=0 then
value = arrList(index)
Else
value = arrList(arrLength - Abs(index))
End If

If Err Then
showErr "错误:" & Err.Description & "【ArrayList.UpdateIndex()】"
Error.Clear
Exit Function
End If
GetValue = value
End Function

'若ArrayList中任一元素等于v,则返回True
Public Function IsInclude(v)
Dim boolIsInclude
boolIsInclude = False
For i = 0 To arrLength - 1
If arrList(i) = v Then
boolIsInclude = True
Exit For
End If
Next
IsInclude = boolIsInclude
End Function

'返回ArrayList中的最大值
Public Function MaxValue()
Dim max
max = arrList(0)
For i = 0 To arrLength - 1
If arrList(i) > max Then
max = arrList(i)
End If
Next
MaxValue = max
End Function

'返回ArrayList中的最小值
Public Function MinValue()
Dim min
min = arrList(0)
For i = 0 To arrLength - 1
If arrList(i) < max Then
min = arrList(i)
End If
Next
MinValue = min
End Function
%>
netbooting 2011-11-28
  • 打赏
  • 举报
回复

<%
'**********************************************
'代码段一
'**********************************************

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

'为arrList赋值
Public Property Let ArrayData(arr)
arrList = arr
arrLength = UBound(arr) + 1
End Property


'******************************************************************************************
'//添加:
'Add(v) 在ArrayList尾部添加一个元素
'AddArray(arr) 在ArrayList尾部附加一个数组
'Insert(index, v) 在ArrayList的index处插入一个值,之后的值都往后移
'InsertArray(index, arr) 在ArrayList的index处插入一个数组,之后的值都往后移
'******************************************************************************************
'在ArrayList尾部添加一个元素
Public Sub Add(v)
ReDim Preserve arrList(arrLength)
arrList(arrLength) = v
arrLength = arrLength + 1
End Sub

'在ArrayList尾部附加一个数组
Public Sub AddArray(arr)
If Not IsArray(arr) Then
showErr "错误:arr参数不是数组【ArrayList.AddArray()】"
Exit Sub
End If
Dim i, j, NewLength
On Error Resume Next
If arrLength = 0 Then '如果ArrayList为空则直接附值
arrList = arr
arrLength = UBound(arr) + 1
Else
NewLength = arrLength + UBound(arr) '新的数组长度
j = 0
ReDim Preserve arrList(NewLength)
For i = arrLength To NewLength
arrList(i) = arr(j)
j = j + 1
Next
arrLength = arrLength + (UBound(arr) + 1)
End If
If Err Then
showErr "错误:" & Err.Description & "【ArrayList.AddArray()】"
Error.Clear
Exit Sub
End If
End Sub

'在ArrayList的index处插入一个值,之后的值都往后移
Public Sub Insert(index, v)
Dim i, v2
If index < arrLength And index >= 0 Then
ReDim Preserve arrList(arrLength)
arrLength = arrLength + 1
For i = index To arrLength - 1
v2 = arrList(i) '交换值
arrList(i) = v
v = v2
Next
Else
showErr "错误:index索引越界【ArrayList.Insert()】"
End If
End Sub

'在ArrayList的index处插入一个数组,之后的值都往后移
Public Sub InsertArray(index, arr)
If index = "" Or Not IsNumeric(index) Then
showErr "错误:非法的index参数【ArrayList.InsertArray()】"
Exit Sub
End If
If index < 0 Or index > arrLength - 1 Then
showErr "错误:index索引越界【ArrayList.InsertArray()】"
Exit Sub
End If
If Not IsArray(arr) Then
showErr "错误:arr参数不是数组【ArrayList.AddArray()】"
Exit Sub
End If

Dim i, j, L1, L2
On Error Resume Next
L1 = UBound(arr)
L2 = arrLength + L1
ReDim Preserve arrList(L2)
For i = arrLength - 1 To index Step -1
arrList(i + L1 + 1) = arrList(i) '把index之后的值往后移
Next
For i = index To index + L1
arrList(i) = arr(j)
j = j + 1
Next
If Err Then
showErr "错误:" & Err.Description & "【ArrayList.InsertArray()】"
Error.Clear
Exit Sub
End If
arrLength = arrLength + L1 + 1 '新的数组长度
End Sub


'******************************************************************************************
'//更新:
'UpdateIndex(index, v) 更新索引为index处的值
'UpdateValue(m, n, v) 将ArrayList中m至n间的元素值替换为v
'ReplaceValue(v, newValue) 将ArrayList中所有值为v的元素替换为newValue
'******************************************************************************************
'更新索引为index处的值
Public Sub UpdateIndex(index, v)
If index = "" Or Not IsNumeric(index) Then
showErr "错误:非法的index参数【ArrayList.UpdateIndex()】"
Exit Sub
End If
If index < 0 Or index > arrLength - 1 Then
showErr "错误:index索引越界【ArrayList.UpdateIndex()】"
Exit Sub
End If
arrList(index) = v
End Sub

'将ArrayList中m至n间的元素值替换为v
Public Sub UpdateValue(m, n, v)
If m = "" Or Not IsNumeric(m) Then
showErr "错误:非法的m参数【ArrayList.UpdateValue()】"
Exit Sub
End If
If n = "" Or Not IsNumeric(n) Then
showErr "错误:非法的n参数【ArrayList.UpdateValue()】"
Exit Sub
End If
If m < 0 Or m > arrLength - 1 Then
showErr "错误:m索引越界【ArrayList.UpdateIndex()】"
Exit Sub
End If
If n < 0 Or n > arrLength - 1 Then
showErr "错误:n索引越界【ArrayList.UpdateIndex()】"
Exit Sub
End If
Dim temp
If m > n Then
temp = m
m = n
n = temp
End If
For i = m To n
arrList(i) = v
Next
End Sub

'将ArrayList中所有值为v的元素替换为newValue
Public Sub ReplaceValue(v, newValue)
For i = 0 To arrLength - 1
If arrList(i) = v Then arrList(i) = newValue
Next
End Sub
%>

netbooting 2011-11-28
  • 打赏
  • 举报
回复
内容太长,居然发不出来。

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧