asp学习网: 首页 >> class:类 >> 数组操作类

数组操作类

Option Explicit

Class cArr
     Private tMin      ''//最小值 thisMin
     Private tMax      ''//最大值
     Private tStr      ''//数组串
     Private tSortStr''//排序串,当对同一数组串进行操作时避免多次排序操作
     Private tLength      ''//元素个数

     Private Sub Class_Initialize
           tMin      =0
           tMax      =0
           tStr      ="0"
           tLength      =0
           tSortStr=""
     End Sub

     ''//获得最小值
     Property Get cMin
           If tMin=0 Then
                 Dim sStr
                 If tSortStr="" Then
                       sStr=Split(cSort,",")
                 Else
                       sStr=Split(tSortStr,",")
                 End If
                 tMin=sStr(0)
           End If
           cMin=tMin
     End Property

     ''//获得最大值
     Property Get cMax
           If tMax=0 Then
                 Dim sStr
                 If tSortStr="" Then
                       sStr=Split(cSort,",")
                 Else
                       sStr=Split(tSortStr,",")
                 End If
                 tMax=sStr(Ubound(sStr))
           End If
           cMax=tMax
     End Property

     Property Get Length
           If tStr="" Or tStr=0 Then
                 Length      =0
           Else
                 tLength      =Ubound(Split(tStr,","))+1
                 Length      =tLength
           End If
     End Property

     ''//将数组串赋给属性
     Property Let srcStr(str)
           If str="" Then
                 tStr="0"
           Else
                 tStr=str
           End If
     End Property

     ''//排序,倒序
     Public Function cSort
           Dim Arr,i
           Redim Arr(Ubound(Split(tStr,",")))
           Dim tmp,j
           For i=0 to Ubound(Arr)
                 Arr(i)=CDBL(Split(tStr,",")(i))
           Next
           For I= 0 to Ubound(Arr)
                 For  j=i+1 to Ubound(Arr)
                       If Arr(i)<Arr(j) Then
                             tmp      =Arr(i)
                             Arr(i)      =Arr(j)
                             Arr(j)      =tmp
                       End If
                 Next
           Next
           tSortStr=Join(Arr,",")
           cSort=tSortStr
     End Function
   
     ''//顺序
     Public Function cSortAsc
           Dim tmp,i,Arr
           If tSortStr="" Then
                 tmp=cSort
           Else
                 tmp=tSortStr
           End If
           Redim Arr(Ubound(Split(tmp,",")))
           Arr=Split(tmp,",")
           For i= Ubound(Arr) to 0 Step -1
                 cSortAsc=cSortAsc & Arr(i) & ","
           Next
           cSortAsc=Left(cSortAsc,Len(cSortAsc)-1)
     End Function

     ''//在未尾加元素
     Public Function AddItem(str)
           If tStr="" OR tStr="0" Then
                 tStr=str
           Else
                 tStr=tStr & "," & str
           End If
           tMin      =0
           tMax      =0
           tSortStr=""
           tLength      =tLength + 1
           AddItem      =tStr
     End Function

     ''//前部加元素
     Public Function AddItemBefore(str)
           If tStr="" OR tStr="0" Then
                 tStr=str
           Else
                 tStr=str & "," & tStr
           End If
           tMin      =0
           tMax      =0
           tSortStr=""
           tLength      =tLength + 1
           AddItemBefore=tStr
     End Function

     ''//移除元素
     Public Function RemoveItem(str)
           tStr      =Replace("," & tStr & "," , "," & str & "," , ",")
           tStr      =Mid(tStr,2,Len(tStr)-2)
           tMin      =0
           tMax      =0
           tSortStr=Replace("," & tSortStr & "," , "," & str & "," , ",")
           tSortStr=Mid(tSortStr,2,Len(tSortStr)-2)
           tLength      =0
           RemoveItem=tStr
     End Function

     ''//在索引位置加元素
     Public Function AddItemI(index,str)
           If index>=Length Then
                 AddItem(str)
                 Exit Function
           End If
           If index<=0 Then
                 AddItemBefore(str)
                 Exit Function
           End if
         
           Dim Arr,i,tmps
           Redim Arr(Ubound(Split(tStr,",")))
           Arr=Split(tStr,",")
           For i=0 to index-1
                 tmps=tmps & Arr(i) & ","
           Next
           tmps=tmps & str & ","
           For i=index to Ubound(Arr)
                 tmps=tmps & Arr(i) & ","
           Next
           tmps      =Left(tmps,Len(tmps)-1)
           tStr      =tmps
           tSortStr=""
           tLength      =tLength+1
           tMin      =0
           tMax      =0
           AddItemI=tStr
     End Function

     ''//在索引位置移除元素
     Public Function RemoveItemI(index)
           If index>=Length Or index<=0 Then
                 Exit Function
           End If
         
           Dim Arr,i,tmps
           Redim Arr(Ubound(Split(tStr,",")))
           Arr=Split(tStr,",")
           For i=0 to Ubound(Arr)
                 If i<>index Then tmps=tmps & Arr(i) & ","
           Next
           tmps=tmps & str & ","
           tmps      =Left(tmps,Len(tmps)-1)
           tStr      =tmps
           tSortStr=""
           tLength      =0
           tMin      =0
           tMax      =0
           RemoveItemI=tStr
     End Function

End Class


使用:
比如传入一个数组元素值和顺序是 17,23,55,99,37,比如存入TheArr变量中
Dim SrcStr:SrcStr=Join(TheArr,",")
Dim ca
Set ca=New cArr
ca.srcStr=SrcStr
取最小值
--ca.cMin
最大值
--ca.cMax
取元素个数
--ca.Length
在未尾增加元素
--NewStr=ca.AddItem("99")
从大到小排序
--Response.Write ca.cSort
从小到大
--ca.cSortAsc
移除值为xx的元素
--ca.MoveItem(xxx)
移除索引值为index的元素
--ca.MoveItemI(index)
在索引值为index的元素前插入一个值为str的元素
--ca.AddItem(index,str)
..
排序操作暂时仅支持数元素值为数字型的数组,仅支持一维 from:asp学习网/title:数组操作类/ time:2006-4-28 16:26:34

本文主题数组操作类

asp教程 ©2006-2007 aspxuexi.com | 关于站点 | 版权隐私 | 站内搜索
复制或者翻版 请于夜间进行