我有一个X元素数组,让我们说X = 50.
如何删除数组的每个第5个元素?
因此,最终结果应该是只剩下40个元素的数组,因为每个第5个元素都被取出....
答案 0 :(得分:1)
下面的代码没有经过测试,但它应该非常接近,在代码运行之后,所有不可被5个元素整除的代码将包含在数组'strSecond'中。
<强>更新强>
以下代码现已经过测试 -
Dim strFirst(49) As String
Dim strSecond() As String
Dim ArrCount As Integer
ArrCount = 0
For i = 1 To 50
strFirst(i - 1) = i
Next
For i = 0 To 49
If (i + 1) Mod 5 <> 0 Then
ReDim Preserve strSecond(ArrCount)
strSecond(ArrCount) = strFirst(i)
ArrCount = ArrCount + 1
End If
Next
For i = 0 To UBound(strSecond)
Debug.Print strSecond(i)
Next
更新II
这适用于多维数组(当您使用Preserve关键字时,只能更改多维数组中最后一个维度的上限) -
Dim strFirst(1, 49) As String
Dim strSecond() As String
Dim ArrCount As Integer
ArrCount = 0
For i = 1 To 50
strFirst(0, i - 1) = i
strFirst(1, i - 1) = i
Next
For i = 0 To 49
If (i + 1) Mod 5 <> 0 Then
ReDim Preserve strSecond(1, ArrCount)
'If you have more than two dimensions could write a loop to do this
'For j = 0 To UBound(strFirst,1) etc
strSecond(0, ArrCount) = strFirst(0, i)
strSecond(1, ArrCount) = strFirst(1, i)
ArrCount = ArrCount + 1
End If
Next
For i = 0 To UBound(strSecond, 2)
Debug.Print strSecond(0, i) + "-" + strSecond(1, i)
Next