我有一个动态整数数组,我希望添加新值.我该怎么做?
Dim TeamIndex(),i As Integer For i = 0 to 100 'TeamIndex(i).Add = <some value> Next
将ReDim与Preserve一起使用可以通过保留旧值来增加数组的大小.
当您不知道大小并且逐渐了解增加数组大小时,建议使用循环ReDim.
Dim TeamIndex(),i As Integer For i = 0 to 100 ReDim Preserve TeamIndex(i) TeamIndex(i) = <some value> Next
如果你稍后在镜头中的代码中声明数组的大小,那么使用
ReDim TeamIndex(100)
所以代码将是:
Dim TeamIndex(),i As Integer ReDim TeamIndex(100) For i = 0 to 100 TeamIndex(i) = <some value> Next
您可以使用ArrayList / List(Of T)更动态地使用添加/删除值.
Sub Main() ' Create an ArrayList and add three strings to it. Dim list As New ArrayList list.Add("Dot") list.Add("Net") list.Add("Perls") ' Remove a string. list.RemoveAt(1) ' Insert a string. list.Insert(0,"Carrot") ' Remove a range. list.RemoveRange(0,2) ' Display. Dim str As String For Each str In list Console.WriteLine(str) Next End Sub