從數組中間刪除一段,將前後兩部分合併為新數組。
Dim src(1 To 100) As Long
Dim dst() As Long
Dim delStart As Long, delEnd As Long
delStart = 30: delEnd = 40 ' 刪除 30-40
Dim newSize As Long
newSize = UBound(src) - LBound(src) + 1 - (delEnd - delStart + 1)
ReDim dst(LBound(src) To LBound(src) + newSize - 1)
Dim i As Long, j As Long
' 複製前段
For i = LBound(src) To delStart - 1
j = j + 1
dst(j) = src(i)
Next
' 複製後段
For i = delEnd + 1 To UBound(src)
j = j + 1
dst(j) = src(i)
Next
' dst 現在為連續下標的新數組
j 寫入目標數組,自動實現下標連續。Collection、Dictionary) 或升級 VB.NET 使用 List(Of T)。