vb.net Visual Basic 中长度未知的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2068458/
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
Arrays of unknown length in Visual Basic
提问by BHurst
I have a piece of code written in Visual Basic:
我有一段用 Visual Basic 编写的代码:
Dim n As Double, i As Integer
n = 4
Dim Ramp_length(1 To 4) As Double
For i = 1 To n
Ramp_length(i) = Cells(13 + i, 5)
'Cells(65 + i, 7) = Ramp_length(i)'
Next i
Is there a way I can reproduce the result without declaring the array with a "fixed" length? I eventually want the code to read off a column of data with unknown length and store it in an array of equal or smaller length so it can be altered. This is just a piece of the code...if someone could help me out that would be great! :D
有没有一种方法可以在不声明具有“固定”长度的数组的情况下重现结果?我最终希望代码读取一列长度未知的数据并将其存储在长度相等或更小的数组中,以便可以对其进行更改。这只是一段代码……如果有人可以帮助我,那就太好了!:D
Thanks
谢谢
回答by SteveGSD
Not sure if this is what you're looking for:
不确定这是否是您要查找的内容:
Dim n As Double = 44
Dim Ramp_Length_List As New List(Of Double)
For i As Integer = 1 To n
Ramp_Length_List.Add(Your_Temp_Double)
Next i
Dim Ramp_Length(Ramp_Length_List.Count - 1) As Double
For i As Integer = 0 To Ramp_Length_List.Count - 1
Ramp_Length(i) = Ramp_Length_List(i)
Next
Ramp_Length_List = Nothing
'Ramp_Length() as Double is now sort of dynamic.
回答by scotty86
You can use ReDim Preserve to resize an array. If you only need it a view times and don't use it in a loop, it's ok. But ReDim Preserve is very slow, because it creates a completely new array and copies the old values into it. If you need to fill an array up with an unknown amount of data sets, use a list. I made a small example:
您可以使用 ReDim Preserve 调整数组大小。如果你只需要一个查看次数而不是循环使用它,那没关系。但是 ReDim Preserve 非常慢,因为它会创建一个全新的数组并将旧值复制到其中。如果您需要用未知数量的数据集填充数组,请使用列表。我做了一个小例子:
Dim num_iterations as Integer
' redim solution
Dim dyn_array As Integer()
For i = 0 To num_iterations - 1
ReDim Preserve dyn_array(i)
dyn_array(i) = i
Next
' list solution
Dim list_for_array As New List(Of Integer)
For i = 0 To num_iterations - 1
list_for_array.Add(i)
Next
Dim list_array As Integer() = list_for_array.ToArray
With 10000 iterations the ReDim-solution has a runtime of 32ms, the list solution is instantly ready (0 ms). 100000 iterations result in 5487ms using ReDim and 1ms using a list.
经过 10000 次迭代,ReDim 解决方案的运行时间为 32 毫秒,列表解决方案立即准备就绪(0 毫秒)。100000 次迭代导致使用 ReDim 为 5487 毫秒,使用列表为 1 毫秒。
回答by sinoj
Dim DynaStrings() As String
ReDim DynaStrings(10)
DynaStrings(0) = "String 0"
'Need more data space!
ReDim DynaStrings(100)
DynaStrings(50) = "String 50"
Hope this might help.
希望这可能会有所帮助。
回答by Bruno Reis
There's no such a thing, array with "dynamic" length.
没有这样的东西,具有“动态”长度的数组。
You can, however, encapsulate some logic that will resize the array whenever needed (this is known as ResizeArray in some languages, List in others, or ArrayList; I'm sorry, I don't know exactly which one in VB; if it's VB.NET then use "List").
但是,您可以封装一些在需要时调整数组大小的逻辑(这在某些语言中称为 ResizeArray,在其他语言中称为 List,或 ArrayList;抱歉,我不知道 VB 中的确切名称;如果是VB.NET 然后使用“列表”)。
回答by keyboardP
You could use an ArrayList. It's the closest you can come to a dynamic array. There are other collections as well, so it may come down to what you expect to do with the data. However, you can't create an array and vary it's length dynamically in vb. (Well, not efficiently :) )
您可以使用ArrayList。这是最接近动态数组的方法。还有其他集合,因此可能归结为您期望对数据做什么。但是,您不能在 vb 中创建数组并动态改变其长度。(好吧,效率不高:))