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、插入数组元素,需要先将数组长度加长再增加数组元素;删除数组元素,可以先删除数组元素,再建数组长度缩短。
扩展资料:
数组中元素的插入和删除一般是在己固定序列的数组中插入或除一个元素,使得插入或删除操作后的数组还是有序的。
1、插入
代码如下:
Private Sub Command1 click)
Dim a(10)As Integer
Dim i As Integer, k As Integer
For i = 0 To 9
a(i) = i*3 +1
Print a(i)
Next i
Print"插入14"
For k =0 To 9
If 14 < a(k) Then Exit For
Next k
For i = 9 To k Step 从最后元素开始逐个后移腾出位置
a(i+1)=a(i)
Next i
Option Explicit
Private Sub Command1_Click()
Dim s, MyArray, a(), ddd
Dim i As Integer
s = Array(3, 1, 1, 1, 1, 1, 1, 4, 3, 5, 3, 6, 6, 6)
ReDim a(0 To UBound(s))
For i = 0 To UBound(s)
MyArray = Filter(s, s(i))
ddd = Filter(a, UBound(MyArray) & s(i))
If UBound(ddd) < 0 Then
a(i) = UBound(MyArray) & s(i)
Print s(i)
End If
Next i
End Sub
代码测试过了,没问题
Option Explicit
Private Sub Command1_Click()
Dim s, MyArray, a(), ddd
Dim i As Integer
s = Array(3, 1, 1, 1, 1, 1, 1, 4, 3, 5, 3, 6, 6, 6)
ReDim a(0 To UBound(s))
For i = 0 To UBound(s)
MyArray = Filter(s, s(i))
ddd = Filter(a, UBound(MyArray) & s(i))
If UBound(ddd) < 0 Then
a(i) = UBound(MyArray) & s(i)
Print s(i)
End If
Next i
End Sub
S = Array(3, 1, 1, 1, 1, 1, 1, 4, 3, 5, 3)
For i = 0 To UBound(S)
For j = 0 To i - 1
If S(i) = S(j) Then Exit For
Next
If j = i Then Print S(i)
Next
End Sub
Dim a(20) As Integer
For i = 0 To 20
a(i) = 10 * Rnd
Print a(i) & " ";
Next
For i = LBound(a) To UBound(a)
b = False
For j = LBound(a) To i - 1
If a(i) = a(j) Then
b = True
Exit For
End If
Next
If Not b Then Print a(i) & " ";
Next
End Sub
广告 您可能关注的内容 |