Arrays 从数组中删除项 如何删除数组中间的一个项?我试过这个: Public Sub RemoveArrayElement(AryVar() As Object, ByVal RemoveWhich As Long) Dim byteLen As Byte byteLen = 4 If RemoveWhich < UBound(AryVar) Then CopyMemory ByVal VarPtr(AryVar(RemoveWhich)), ByVal _ VarPtr(AryVar(RemoveWhich + 1)), (byteLen) * _ (UBound(AryVar) - RemoveWhich) End If If UBound(AryVar) = LBound(AryVar) Then Erase AryVar Else ReDim Preserve AryVar(UBound(AryVar) - 1) End If End Sub 公共子RemoveArrayElement(AryVar()作为对象,ByVal RemoveWhich作为长) Dim byteLen作为字节 byteLen=4 如果移除哪个
但是,当我从Arrays 从数组中删除项 如何删除数组中间的一个项?我试过这个: Public Sub RemoveArrayElement(AryVar() As Object, ByVal RemoveWhich As Long) Dim byteLen As Byte byteLen = 4 If RemoveWhich < UBound(AryVar) Then CopyMemory ByVal VarPtr(AryVar(RemoveWhich)), ByVal _ VarPtr(AryVar(RemoveWhich + 1)), (byteLen) * _ (UBound(AryVar) - RemoveWhich) End If If UBound(AryVar) = LBound(AryVar) Then Erase AryVar Else ReDim Preserve AryVar(UBound(AryVar) - 1) End If End Sub 公共子RemoveArrayElement(AryVar()作为对象,ByVal RemoveWhich作为长) Dim byteLen作为字节 byteLen=4 如果移除哪个,arrays,vb6,Arrays,Vb6,但是,当我从aryvar检索项时,它返回Nothing的方法与上面的方法稍有不同,但您不能将所有值向下移动,然后收缩数组吗 例如: Public Sub RemoveArrayElement(AryVar() As Object, ByVal RemoveWhich As Long) If UBound(AryVar) > 0 Then Dim i As Integer For i = LBound(AryVar) To UBound(AryVar)
aryvar
检索项时,它返回Nothing
的方法与上面的方法稍有不同,但您不能将所有值向下移动,然后收缩数组吗
例如:
Public Sub RemoveArrayElement(AryVar() As Object, ByVal RemoveWhich As Long)
If UBound(AryVar) > 0 Then
Dim i As Integer
For i = LBound(AryVar) To UBound(AryVar) - 1
If i >= removeWhich Then
AryVar(i) = AryVar(i + 1)
End If
Next
ReDim Preserve AryVar(UBound(AryVar) - 1)
End If
End Sub
另一种方法是:
'1 form with:
' 1 command button: name=Command1
Option Explicit
Private Sub Command1_Click()
Dim intIndex As Integer
'dim an array with undefined boundaries
Dim intArray() As Integer
'set boundaries
ReDim intArray(10) As Integer
'fill array with values
For intIndex = 0 To 10
intArray(intIndex) = intIndex * intIndex
Next intIndex
'show the data from the initial array
ShowArray intArray
'remove the 6th item (index=5)
intArray = RemoveItem(5, intArray)
'show the data of the resulting array (with the 6h item removed)
ShowArray intArray
End Sub
Private Function RemoveItem(intItem As Integer, intSrc() As Integer) As Integer()
Dim intIndex As Integer
Dim intDest() As Integer
Dim intLBound As Integer, intUBound As Integer
'find the boundaries of the source array
intLBound = LBound(intSrc)
intUBound = UBound(intSrc)
'set boundaries for the resulting array
ReDim intDest(intLBound To intUBound - 1) As Integer
'copy items which remain
For intIndex = intLBound To intItem - 1
intDest(intIndex) = intSrc(intIndex)
Next intIndex
'skip the removed item
'and copy the remaining items, with destination index-1
For intIndex = intItem + 1 To intUBound
intDest(intIndex - 1) = intSrc(intIndex)
Next intIndex
'return the result
RemoveItem = intDest
End Function
Private Sub ShowArray(intArray() As Integer)
Dim intIndex As Integer
'print all items to the form to show their value
For intIndex = LBound(intArray) To UBound(intArray)
Print "Item " & CStr(intIndex) & " : " & CStr(intArray(intIndex))
Next intIndex
'print an empty line to separate the arrays in displaying
Print
End Sub
区别在于:
- 返回数组而不是传递byref
- 使用暗显为正确大小的临时阵列,而不是在末尾使用redim PREVICE
- 复制保留相同索引的项目,并仅移动删除项目后的项目
- 这个代码片段可能会对您有所帮助
Dim remove_place As Integer
Dim ar() As Integer
Dim n As Integer
n = 10
ReDim ar(n) 'Initial size of an array
remove_place = Val(Text1.Text) 'This is the position to be deleted
For i = remove_place To n - 1
ar(i) = ar(i + 1) 'This loop will shift array position one place to the left. This way the number at your position will be replaced by the next number.
Next
ReDim Preserve ar(n - 1) 'I guess you know what it means.
将其置于任何事件过程下。这看起来像vb.net。问题被标记为vb6。@C-PoundGuru它确实是vb6,vb6中不支持“i作为整数”为什么有文本框?我没听说过,这只是一个演示。我使用文本框通过文本框获取用户的位置。你可以采取任何其他选择。那要看你了。这个程序只是为了演示“删除”算法。