除非您可以使用Collection
对象,否则没有内置或直接的方法来对数组执行此操作。
Public Sub TestArr()
Dim list As New Collection
list.Add "John"
list.Add "Mary"
list.Add "Bob"
list.Remove 1
End Sub
随着阵列可以使用Preserve
关键字,以保持现有的数据ReDim
他们。
Public Sub TestArr()
Dim list() As Variant
'Creates a dynamic list of 10 items
ReDim list(1 To 10)
Dim i As Long
For i = 1 To 10
list(i) = "Item" & CStr(i)
Next i
'Append to the end of an array
AppendToArray list, "NotAnItem"
'Remove the 5th element
RemoveFromArray list, 5
End Sub
Public Sub AppendToArray(ByRef list() As Variant, ByVal item As Variant)
Dim n As Long
n = UBound(list)
' Make Room
ReDim Preserve list(1 To n + 1)
' Assign new value
list(n + 1) = item
End Sub
Public Sub RemoveFromArray(ByRef list() As Variant, ByVal index As Long)
Dim n As Long, i As Long
n = UBound(list)
' Move last items down by one
For i = index To n - 1
list(i) = list(i + 1)
Next i
ReDim Preserve list(1 To n - 1)
End Sub
如果我明白正确的话,您应该使用堆栈,这对于Java也是如此。在VBA中,您可以使用此创建一个 - > Set stack = CreateObject(“System.Collections.Stack”),其中stack是类型对象的变量。 –
https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx#Anchor_10 –