编一对已知数组a(),删除数组中某个元素的程序.

假定数组a的值利用array()函数赋给,分别为{1,6,8,3,5,9,10,2,7,4},删除key值为10的元素素
提示:要删除元素,必须分三步完成,首先查找到要删除的元素。然后从下一个元素到最后元素一次往前移位,最后利用redim preserve语句将数组大小减1
界面要怎么设计?

第1个回答  2009-05-10
Private Sub Command1_Click()
Dim a As Variant
a = Array(1, 6, 8, 3, 5, 9, 10, 2, 7, 4)
For i = 0 To UBound(a)
If a(i) = 10 Then
For j = i To UBound(a) - 1
a(j) = a(j + 1)
Next j
a(j) = a(i)
Exit For
End If
Next i
ReDim Preserve a(UBound(a) - 1)
For i = 0 To UBound(a)
Debug.Print a(i);
Next i
End Sub本回答被提问者采纳
第2个回答  2009-05-10
Private Sub Command1_Click()
Dim a, i As Integer, j As Integer
a = Array(1, 6, 8, 3, 5, 9, 10, 2, 7, 4)
For i = LBound(a) To UBound(a) '第一步:定位
If a(i) = 10 Then Exit For
Next
If i > UBound(a) Then Exit Sub '找不到则退出
For j = i + 1 To UBound(a) '第二步:移位
a(j - 1) = a(j)
Next
ReDim Preserve a(UBound(a) - 1) '第三步:利用redim preserve语句将数组大小减1
第3个回答  2009-05-10
Dim i As Integer, a, j As Integer
a = Array(1, 6, 8, 3, 5, 9, 10, 2, 7, 4)
For i = LBound(a) To UBound(a)
Debug.Print a(i);
Next i
Debug.Print
For i = LBound(a) To UBound(a)
If a(i) = 10 Then
For j = i To UBound(a) - 1
a(j) = a(j + 1)
Next j
ReDim Preserve a(UBound(a) - 1)
Exit For
End If
Next i
For i = LBound(a) To UBound(a)
Debug.Print a(i);
Next i
Debug.Print
相似回答