asp 动态数组 提供Add、Insert、Remove、RemoveAt、Search等方法。

代码如下:

Class Vector

Private vector_datas()
Private initial_capacity '初始化容量
Private capacity_increment '容量增量
Private element_count '元素数
Private max_capacity '总容量

Private Sub Class_Initialize()
RemoveAll
End Sub

Public Function RemoveAll()
element_count = 0
initial_capacity = 10
capacity_increment = 10
max_capacity = initial_capacity
ReDim vector_datas(initial_capacity)
End Function

Public Property Get Count()
Count = element_count
End Property

Public Property Get Capacity()
Capacity = max_capacity
End Property

Public Property Get InitialCapacity()
InitialCapacity = initial_capacity
End Property

Public Property Get CapacityIncrement()
CapacityIncrement = capacity_increment
End Property

Public Default Property Get Item(index)
If IsObject(vector_datas(index)) Then
Set Item = vector_datas(index)
Else
Item = vector_datas(index)
End If
End Property

Public Function Add(element)
Call Insert(element_count, element)
End Function

Public Function Remove(element)
Dim index
index = Search(element)
RemoveAt(index)
Remove = index
End Function

Public Function RemoveAt(index)
Dim i
For i = index + 1 To element_count - 1 Step 1
Call InternalElement(i - 1, vector_datas(i))
Next
element_count = element_count - 1
If max_capacity - capacity_increment > element_count Then
max_capacity = max_capacity - capacity_increment
ReDim Preserve vector_datas(max_capacity)
End If
End Function

Public Function Search(element)
Dim i
For i = 0 To element_count - 1 Step 1
If vector_datas(i) = element Then
Search = i
Exit Function
End If
Next
Search = -1
End Function

Public Function Insert(index, element)
If index > element_count Then
Err.Raise 20903, "Vector", "Array Index Out Of Bounds.", "", 0
End If
If element_count = 0 Then
Call InternalElement(0, element)
ElseIf index = element_count Then
Call InternalElement(element_count, element)
Else
Dim i
For i = element_count To index + 1 Step -1
Call InternalElement(i, vector_datas(i - 1))
Next
Call InternalElement(index, element)
End If
element_count = element_count + 1
If element_count = max_capacity Then
max_capacity = element_count + capacity_increment
ReDim Preserve vector_datas(max_capacity)
End If
End Function

Public Function SetElementAt(index, element)
If index < 0 Or index > element_count - 1 Then
Err.Raise 20903, "Vector", "Array Index Out Of Bounds.", "", 0
End If
Call InternalElement(index, element)
End Function

Private Function InternalElement(index, element)
On Error Resume Next
If IsObject(element) Then
Set vector_datas(index) = element
Else
vector_datas(index) = element
End If
If Err.Number <> 0 Then
MsgBox("Vector InternalElement Error: " & vbCrLf & "Error Source: " & Err.Source & vbCrLf & "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf)
Err.Clear '清除错误信息
End If
End Function

Private Sub Class_Terminate() '类销毁
Erase vector_datas '释放数组占用的内存, 將每個元素都設為 Nothing
initial_capacity = Empty
capacity_increment = Empty
element_count = Empty
max_capacity = Empty
End Sub

End Class

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/o1o2o3o4o5/archive/2009/10/20/4703033.aspx

(0)

相关推荐

  • asp.net 字符串、二进制、编码数组转换函数

    1.字符串转二进制数组 string content="这是做个测试!"; System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] byteArr = converter.GetBytes(content); 2.二进制数组转为字符串 复制代码 代码如下: System.Text.UnicodeEncoding converter = new System.Text.Unicod

  • ASP 使用Filter函数来检索数组的实现代码

    语法说明: Filter 函数 返回下标从零开始的 数组,其中包含以特定过滤条件为基础的字符串数组的子集. Filter(InputStrings, Value[, Include[, Compare]]) 参数 InputStrings 必选项.一维数组,要在其中搜索字符串. Value 必选项.要搜索的字符串. Include 可选项.Boolean 值,指定返回的子字符串是否包含 Value.如果 Include 为 True,Filter 将返回包含子字符串 Value 的数组子集.如果

  • asp中使用redim、preserve创建动态数组实例

    asp中REDIM的功能是动态定义数组长度 动态数组里面的一个语句,只能出现在过程里面,可以多次使用.可以改变数组大小,和维数. 格式: REDIM [Preserve] 数组名(下标1[下标2....]) Preserve 保留动态数组的内容(不用的话,每次执行REDIM语句,当前存储的语句会全部丢失) 例如: 复制代码 代码如下: Dim DynArray() '定义数组DynArray()为动态数组 REDIM Preserve DynArray(20)'为该数组分配元数个数 这样对编程中

  • ASP 过滤数组重复数据函数(加强版)

    函数代码: 复制代码 代码如下: <%'******************************************************* '过滤数组重复函数名称:array_no(cxstr1,cxstr2,cxstr3) 'cxstr1:任意的字符串,自动识别 'cxstr2:cxstr1中分割符号. 'cxstr3:提取结果中的某一位置字串,等于0时返回为全部,大于数组下标时返回最后. '使用于二维数组 '************************************

  • ASP定义数组方法的技巧

    数组是有序数据的集合.数组中的元素可以不属于同一个数据类型.用一个统一的数组名和下标来唯一地确定数组中的元素,更改其中一个元素并不会影响其它元素.数组的下标是有界的,分为下界和上界.数组可以用Dim.Private.Public或Static来声明,它们的语法格式相同.下面只介绍用Dim声明数组的方法. 1.数组的定义与声明 数组的定义语法如下: Dim 数组名( [[下标下界 To ] 下标上界] ) [As 数据类型] 例如(假设在当前模块中 数组的缺省下界为0)): ① Dim A(10)

  • asp.net通过js实现Cookie创建以及清除Cookie数组的代码

    复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="BLTZ.aspx.cs" Inherits="BLTZ" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1

  • asp取得数组中的最大值的方法

    如何取得数组中的最大值(由71port_80端口提供)   该函数的作用是取得一组数组中最大的一个值,非常实用且精典,值得收藏! 复制代码 代码如下: snum="345,231,56,786,1100,356,1200,300,685,111,134,765" function GetMax(str)  num=split(str,",")  max=num(0)  for ii=0 to ubound(num)  if cint(num(ii))>cint

  • asp下使用数组存放数据的代码

    asp用客户端数组存放数据,这种应用我已经见过很多.但最近在研究几套流量交换联盟系统时,这种技术才引起了我的注意. 下面我讲讲如何运用,先给出个没有结合ASP,就单纯适用javascript的例子. 1.js 复制代码 代码如下: var a = new Array();  var temp = "<table>";  a[0] = new Array(1,"阿会楠","男");   a[1] = new Array(2,"

  • Asp与JS的数组和字符串下标介绍

    数组都是从0开始.javascript是arrayname[i],而vbscript是arrayname(i) javascript的字符串还是从0开始,比如取第一个字符,stringname.charAt(0). var s="abcd"; s.indexOf("b")返回1 asp的字符串下标从1开始.比如取第一个字符:mid(stringname,1,1). 再比如:s="abcd" instr(s,"b")返回2. 另

  • asp.net 数组中字符串替换的几种方式

    复制代码 代码如下: //方法1 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ReplaceStr(); } } public void ReplaceStr() { ArrayList MyArray = new ArrayList(); MyArray.Add("123"); MyArray.Add("aaa"); if (MyArray.Conta

  • asp 数组 重复删除函数(脚本之家增强版)

    因为要写个东西用到,所以百度了一下,居然有朋友乱写,而且比较多,都没有认真测试过,只对字符可以,但是对数字就不可以,而且通用性很差,需要修改才可以真正使用.没办法就自己写了,经过测试完全没有问题,而且思路很方便,代码很短,如下: 复制代码 代码如下: <% function cxarraynull(cxstr1,cxstr2) if isarray(cxstr1) then cxarraynull = "对不起,参数1不能为数组" Exit Function end if if

  • asp数组的使用介绍

    定义简单数组 有两种方法在asp中定义和初始化数组,让我们看看每种的例子: 方法一: MyArray = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec") 数组

  • asp 得到动态数组中元素的个数

    一个动态数组 a,如果你已经使用redim 语句给它设定了大小,那么在此之后使用 ubound(a) 就可以得到它的上边界. 如果你没有使用 redim 语句给它设定大小,直接使用 ubound(a) 函数,那么运行时会报错,并会中断程序的执行.我们恰恰利用这一点,可以知道这个数组还没有任何元素.于此同时,我们却不想程序中断执行,那么可以在 ubound(a) 函数执行前加上一句 复制代码 代码如下: on error resume next 把本功能写成一个函数 function get_el

  • asp textarea 多行数组分割处理方法

    所以对应的asp处理代码如下 复制代码 代码如下: dedearr=split(xiangguanid2,chr(13)) '分割成数组for dede=0 to ubound(dedearr)-1 '数组长度减一,因为最后有两个chr(13)换行.dedearr2=split(dedearr(dede),"|") dedetitle=dedearr2(1) dedeurl=dedearr2(0) if dedetitle<>"" and dedeurl

随机推荐