程序代码如下:
创新互联建站是专业的灵寿网站建设公司,灵寿接单;提供成都做网站、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行灵寿网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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
'建立一个command1
dim shu() as integer
dim i as integer
private sub form_load()
form1.autoredraw=true
'此处n、c以及count(n)数组各元素的输入略去
end sub
private sub command1_click()
redim shu(c)
for i=1 to c
shu(i)=0
next
for i=1 to n
shu(count(i))=shu(count(i))+1
next
for i=1 to c
print "元素";i;"的个数是:";shu(i)
next
end sub
array(2,2)是多维数组的访问方式,其数组的定义和初始化方法为:
Dim array As Integer(,) = {{1, 2, 3}, {4, 5, 6}}
每一行的元素数量是固定且相等的。
array(2)(2)是交错数组(即数组的数组)的访问方式。也就是,你有一个数组,这个数组的每个元素也是数组。其数组的定义和初始化方法为:
Dim array As Integer()() = {New Integer() {1, 2, 3}, New Integer() {4, 5, 6, 7, 8}}
由于每个元素是独立的数组,所以交错数组每一行的元素数量不固定,且可以不等。
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()。