Sub
Command1_Click()
Dim
x()
As
Integer,
i%,
n%,
k%
ReDim
x(1
To
10)
As
Integer
Form1.Cls
For
i
=
1
To
10
x(i)
=
Int(Rnd()
*
10)
x(i)
Next
i
n
=
Val(Text1)
'插入数字
k
=
Val(Text2)
'插入位置
If
n
>
0
And
k
>
0
And
k
<
11
Then
ReDim
Preserve
x(1
To
11)
As
Integer
For
i
=
(UBound(x)
-
1)
To
(k
-
1)
Step
-1
x(i
+
1)
=
x(i)
Next
i
x(k)
=
n
For
i
=
1
To
11
x(i)
Next
i
Else
MsgBox
"输入数字有误",
vbCritical
End
If
End
Sub
Dim st As String = ""Dim a(9) As Integer, aa As Integer
Dim i As Integer, n As Integer
For i = 0 To 8
a(i) = i + 1
Next
aa = Val(InputBox(" 请输入一个整数"))
n = Int(10 * Rnd())
For i = 8 To n - 1 Step -1
a(i + 1) = a(i)
Next
a(n) = aa
For i = 0 To 9
st = st & a(i) & " "
Next
TextBox6.Text = st.ToString
给你一个方法,但是代码来不及帮你写了1、数组自己可以定义成不定大小的,也就是可以修改上限的,记住要设置成可以保留原数据的那种
2、将数组的上限增加一个,原来有n个元素,现在修改为n+1
3、将需要插入位置及以后位置的数据用另外的数组保存
4、将需要插入的数据赋值给原来数组对应的位置,然后将保存的数据重新赋值回来
(3和4步,可以不用新的数组实现:将第n个数据赋值到第n+1个,第n-1个数据赋值到第n个、第n-2个数据赋值到第n-1个,依次类推,直到所有需要转移的数据全部 *** 作完毕既可)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)