vb.net 向整数数组添加新值 (Visual Basic 2010)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10578993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add new value to integer array (Visual Basic 2010)
提问by antikbd
I've a dynamic integer array to which I wish to add new values. How can I do it?
我有一个动态整数数组,我希望向其中添加新值。我该怎么做?
Dim TeamIndex(), i As Integer
For i = 0 to 100
'TeamIndex(i).Add = <some value>
Next
回答by Romil Kumar Jain
Use ReDim with Preserve to increase the size of array with preserving old values.
使用 ReDim 和 Preserve 来增加数组的大小并保留旧值。
ReDim in loop is advisable when you have no idea about the size and came to know for increasing the Array size one by one.
当您不知道大小并逐渐了解如何逐一增加数组大小时,建议使用循环中的 ReDim。
Dim TeamIndex(), i As Integer
For i = 0 to 100
ReDim Preserve TeamIndex(i)
TeamIndex(i) = <some value>
Next
If you to declare the size of array at later in code in shot then use
如果您稍后在代码中声明数组的大小,请使用
ReDim TeamIndex(100)
So the code will be :
所以代码将是:
Dim TeamIndex(), i As Integer
ReDim TeamIndex(100)
For i = 0 to 100
TeamIndex(i) = <some value>
Next
You can Use the ArrayList/List(Of T) to use Add/Remove the values more dynamically.
您可以使用 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
回答by Tony Dallimore
There is nothing in Romil's answer that I consider to be wrong but I would go further. ReDim Preserve
is a very useful command but it is important to realise that it is an expensive command and to use it wisely.
Romil 的回答中没有任何我认为是错误的,但我会走得更远。 ReDim Preserve
是一个非常有用的命令,但重要的是要意识到它是一个昂贵的命令并明智地使用它。
Consider:
考虑:
Dim TeamIndex(), i As Integer
For i = 0 to 100
ReDim Preserve TeamIndex(i)
TeamIndex(i) = <some value>
Next
For every loop, except i=0, the Common Language Runtime (CLR) must:
对于每个循环,除了 i=0,公共语言运行时 (CLR) 必须:
- find space for a new integer array that is one element bigger than the previous array
- copy the values across from the previous array
- initialise the new element
- release the previous array for garbage collection.
- 为比前一个数组大一个元素的新整数数组找到空间
- 复制前一个数组中的值
- 初始化新元素
- 释放之前的数组以进行垃圾回收。
ArrayList
is fantastic if you need to add or remove elements from the middle of the array but you are paying for that functionality even if you do not need it. If, for example, you are reading values from a file and storing them sequentially but do not know in advance how many values there will be, ArrayList
carries a heavy overhead you can avoid.
ArrayList
如果您需要从数组中间添加或删除元素,那就太棒了,但即使您不需要它,您也要为该功能付费。例如,如果您从文件中读取值并按顺序存储它们,但事先不知道将有多少个值,ArrayList
则可以避免大量开销。
I always use ReDim
like this:
我总是这样使用ReDim
:
Dim MyArray() As XXX
Dim InxMACrntMax As Integer
ReDim MyArray(1000)
InxMACrntMax=-1
Do While more data to add to MyArray
InxMACrntMax = InxMACrntMax + 1
If InxMACrntMax > UBound(MyArray) Then
ReDim Preserve MyArray(UBound(MyArray)+100)
End If
MyArray(InxMACrntMax) = NewValue
Loop
ReDim MyArray(InxMACrntMax) ' Discard excess elements
Above I have used 100 and 1000. The values I pick depend on my assessment of the likely requirement.
上面我使用了 100 和 1000。我选择的值取决于我对可能需求的评估。