重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Dim array1() As String
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了湖南免费建站欢迎大家使用!
Dim array2() As Boolean
Dim i As Integer
Dim j As Integer
Dim str1 As String = "a,c,c,c,a,b,c,d,f,eee,eee"
array1 = Split(str1, ",")
str1 = ""
ReDim array2(UBound(array1))
For i = 0 To UBound(array1)
array2(i) = False
Next
For i = 0 To UBound(array1) - 1
If array2(i) = False Then
For j = i + 1 To UBound(array1)
If array1(i) = array1(j) And array2(j) = False Then
array2(j) = True
End If
Next j
End If
Next i
For i = 0 To UBound(array1)
If array2(i) = False Then
str1 = str1 array1(i) ","
End If
Next i
If str1 "" Then
str1 = Strings.Left(str1, Len(str1) - 1)
End If
程序代码如下:
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
比如数组a。里面有5个值,其中有3个是重复的?
如果这样的话,很好办哦。
新建一个数组b,然后遍历要去除的数组a,
从a中把每一个都取出来,和新建的b里面的去比,如果有相同的,则不放入b,
否则就放入数组b,直到循环结束。
Private Function NewArray(ByRef b() As Integer)
Dim Temp() As Integer
Dim H As Integer, I As Integer, J As Integer, K As Integer
Dim IsNew As Boolean
H = UBound(b())
ReDim Temp(0) '如果你原数组元素数量是万个级别的,就直接ReDim Temp(H),下面 ReDim Preserve Temp(K) 去掉
Temp(0) = b(0)
For I = 0 To H
IsNew = True
For J = 0 To K
If Temp(J) = b(I) Then
IsNew = False
Exit For
End If
Next J
If IsNew Then
K = K + 1
ReDim Preserve Temp(K)
Temp(K) = b(I)
End If
Next I
ReDim b(K)
For I = 0 To K
b(I) = Temp(I)
Next I
Erase Temp()
End Function
'调用
'dim b() as integer
'dim I as integer
'dim OutputStr as String
'redim b(Ubound(a()))
'for I=0 TO Ubound(a())
' b(I)=a(I)
'next I
'NewArray(b())
'返回的b()为除重后的数组,可以根据你的需要显示、输出数组a()、b()。