VBS ArrayList Class vbs中的数组类
程序员文章站
2022-03-07 12:38:12
class arraylist private items() private size pr...
class arraylist
private items()
private size
private sub class_initialize
size = 0
redim items(1)
end sub
private sub class_terminate
items = null
end sub
public function add(byval value)
if (size = ubound(items)) then ensurecapacity((size + 1))
items(size) = value
size = size + 1
add = size
end function
public property get item(index)
item = items(index)
end property
public property let item(index, vobject)
items(index) = vobject
end property
property get count
count = size
end property
public property get capacity()
capacity = ubound(items)
end property
public property let capacity(value)
if (value <> ubound(items)) then
if (value < size) then err.rise 6
if (value > 0) then
redim preserve items(value)
else
redim preserve items(3)
end if
end if
end property
private sub ensurecapacity(byval min)
if (ubound(items) < min) then
dim num1 : num1 = iif((ubound(items) = 0), 4, (ubound(items) * 2))
if (num1 < min) then num1 = min
capacity = num1
end if
end sub
private function iif(j, r1, r2)
if (j) then
iif = r1
else
iif = r2
end if
end function
end class
示例:
dim al : set al = new arraylist
al.add(1)
al.add(2)
al.add(3)
al.add(4)
al.add(5)
al.add(6)
al.add(7)
al.add(8)
al.add(9)
al.add(10)
for i = 0 to al.count -1
w("index"& i &": "& al.item(i))
next
w("count: "& al.count)
w("capacity: "& al.capacity)
sub w(o)
response.write(o &"<br />")
end sub
private items()
private size
private sub class_initialize
size = 0
redim items(1)
end sub
private sub class_terminate
items = null
end sub
public function add(byval value)
if (size = ubound(items)) then ensurecapacity((size + 1))
items(size) = value
size = size + 1
add = size
end function
public property get item(index)
item = items(index)
end property
public property let item(index, vobject)
items(index) = vobject
end property
property get count
count = size
end property
public property get capacity()
capacity = ubound(items)
end property
public property let capacity(value)
if (value <> ubound(items)) then
if (value < size) then err.rise 6
if (value > 0) then
redim preserve items(value)
else
redim preserve items(3)
end if
end if
end property
private sub ensurecapacity(byval min)
if (ubound(items) < min) then
dim num1 : num1 = iif((ubound(items) = 0), 4, (ubound(items) * 2))
if (num1 < min) then num1 = min
capacity = num1
end if
end sub
private function iif(j, r1, r2)
if (j) then
iif = r1
else
iif = r2
end if
end function
end class
示例:
dim al : set al = new arraylist
al.add(1)
al.add(2)
al.add(3)
al.add(4)
al.add(5)
al.add(6)
al.add(7)
al.add(8)
al.add(9)
al.add(10)
for i = 0 to al.count -1
w("index"& i &": "& al.item(i))
next
w("count: "& al.count)
w("capacity: "& al.capacity)
sub w(o)
response.write(o &"<br />")
end sub