+ -
当前位置:首页 → 问答吧 → ASP版ArrayList类,请高手指正。

ASP版ArrayList类,请高手指正。

时间:2011-11-28

来源:互联网

VBScript code

<%
'**********************************************
'类名:ArrayList
'作者:[email protected]
'日期: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)          求两个数组的并集
'
%>

作者: netbooting   发布时间: 2011-11-28

内容太长,居然发不出来。

作者: netbooting   发布时间: 2011-11-28

VBScript code

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

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


VBScript code

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

'******************************************************************************************
'//删除:
'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

看了一下接口
最好再提供一个遍历的接口
实现一个迭代器

作者: aspwebchh   发布时间: 2011-11-28