VB编程:数组中删除数组元素
运行效果:
程序代码:
Dim a()
Private Sub Form_Load()
ReDim Preserve a(1 To 5)
For i = 1 To 5
a(i) = i
Next i
For i = 1 To 5
Text1.Text = Text1.Text & "a(" & i & ")=" & a(i) & "; "
Next i
For i = 2 To 4
a(i) = a(i + 1)
Next i
ReDim Preserve a(1 To 4)
For i = 1 To 4
Text2.Text = Text2.Text & "a(" & i & ")=" & a(i) & "; "
Next i
End Sub
总结:
1、插入数组元素和删除数组元素都使用循环交换,插入数组元素是原数组元素往后移动,删除数组元素是将原数组的元素往前移动。
2、插入数组元素,需要先将数组长度加长再增加数组元素;删除数组元素,可以先删除数组元素,再建数组长度缩短。