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
数组名就是指向数组的指针,并且这个指针是不能改变的,即:定义 int buf[10],则buf就是指向这个数组第一个元素的指针,并且不能通过buf=buf+1这样的形式改变buf的指向.要设置指向数组空间的指针很简单,定义int *ptrptr=buf就可以了.并且ptr+1指向第二个数组元素,依次类推.用指针和数组下标可以随意指向任何数组元素.如buf[8], *(ptr+6)等,并不是只能顺序访问.
另外,数组是一块连续的内存空间,这和链表是两回事.
不知道这样解释符合楼主的意思吗?
function insert(arrfirst,arrlast,index){ //将数组arrlast插入数组arrfirst中,index是想要插入的位置if (index < 0){
index = 0
}else if(index > arrfirst.length){
index = arrfirst.length
}
var arr = []
for (var i = 0 i < index i++){
arr.push(arrfirst[i])
}
for (var i = 0 i < arrlast.length i++){
arr.push(arrlast[i])
}
for (var i = index i < arrfirst.length i++){
arr.push(arrfirst[i])
}
return arr
}
var arr1 = ["1","2","3"]
var arr2 = ["a","b","c"]
console.log(insert(arr1,arr2,0))
alert(insert(arr1,arr2,1))
还有一种方法
function insert(arrfirst,arrlast,index){if (index < 0){
index = 0
}else if(index > arrfirst.length){
index = arrfirst.length
}
for (var i = arrlast.length-1 i >= 0 i--){
arrfirst.splice(index,0,arrlast[i])//是在index位置用arrlast[i]替换掉arrfirst数组中的0个元素
}
return arrfirst
}
var arr1 = ["1","2","3"]
var arr2 = ["a","b","c"]
console.log(insert(arr1,arr2,0))
alert(insert(arr1,arr2,1))
希望能帮到你
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)