VB中怎样将新添元素加入数组

如题所述

例如数组1到7位置存有数,要插入元素到数组第5个位置,先把第7个,第6个,第5个依次后移1个位置,然后把要新添的元素赋到数组第5个位置。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-23
使用VB的动态数组:
Dim d() AS Integer
dim n As Integer
n=2
Redim d(n) As Integer '元素下标增加到2
n = 100
Redim d(n) As Integer '元素下标增加到100
注意:用关键字Preserve可以在扩大数组规模的同时,保持原来数组元素的数据不变!!!
n=2
Redim d(n) As Integer '元素下标增加到2
d(0)=0:d(1)=1:d(2)=2
n = 100
Redim d(n) As Integer '元素下标增加到100
这样,原来d(0)d(1)d(2)里数据为0了
如果用
Redim Preserve d(n) As Integer
这样,原来d(0)d(1)d(2)里数据保持不变本回答被网友采纳
相似回答