|
程序既是数据结构和算法。在程序中,数组和链表两种数据结构用得最多。但是,VBA数组功能不是很强大,所以需要进行一些增强。那么首先,我们用VBA中的Array,实现一维列表。具体实现append slice insert remove pop几个function。类定义如下(详见附件):
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsList"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Public self
Private Property Get lb() '上标
lb = LBound(self)
End Property
Private Property Get ub() '下标
ub = UBound(self)
End Property
Public Property Get length() '长度
length = ub - lb + 1
End Property
Public Sub init(array_length) '初始化,如果需要的话
Dim i As Long
ReDim self(0 To array_length - 1)
For i = 0 To array_length - 1
self(i) = i
Next i
End Sub
Public Sub append(append_item)
ReDim Preserve self(lb To ub + 1)
self(ub) = append_item 'isobject=false先不考虑item为object的情况
End Sub
Public Sub remove(remove_index)
Dim i As Long
For i = remove_index To ub - 1
self(i) = self(i + 1)
Next i
ReDim Preserve self(lb To ub - 1)
End Sub
Public Function pop(pop_index) '删除的同时得到删除的值
pop = self(pop_index)
remove pop_index
End Function
Public Sub insert(insert_item, insert_index)
Dim i As Long
ReDim Preserve self(lb To ub + 1)
For i = ub To insert_index + 1 Step -1
self(i) = self(i - 1)
Next i
self(insert_index) = insert_item
End Sub
Public Function slice(start_index, end_index) '切片
Dim i, slice_array
ReDim slice_array(lb To lb + end_index - start_index)
For i = start_index To end_index
slice_array(lb - start_index + i) = self(i)
Next i
slice = slice_array
End Function
|
评分
-
1
查看全部评分
-
|