欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-05 19:32:31
复制代码 代码如下:class vector private vector_datas() private initial_capacity '初始化容量 private...
复制代码 代码如下:

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