VB.NET - 整数数组需要实例化,怎么做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11731082/
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
VB.NET - Array of Integers needs to be instantiated, how to?
提问by Dayan
First try
第一次尝试
Dim holdValues() As Integer 'Doesn't Work
holdValues(1) = 55
Second try
第二次尝试
Dim holdValues(-1) As Integer 'Gives me Index was outside the bounds of the array.
holdValues(1) = 55
I'm trying to do something similar to
我正在尝试做类似的事情
Dim myString(-1) As String
But apparently this doesn't apply to integer arrays. I don't know what the size of the array will be, it wont get smaller but it will grow larger.
但显然这不适用于整数数组。我不知道数组的大小是多少,它不会变小但会变大。
Any help will be appreciated, thank you!
任何帮助将不胜感激,谢谢!
回答by Michael Rodrigues
You could use the Initializers shortcut:
您可以使用 Initializers 快捷方式:
Dim myValues As Integer() = New Integer() {55, 56, 67}
But if you want to resize the array, etc. then definately have a look at a List(Of Integer):
但是如果你想调整数组的大小等等,那么肯定看看一个 List(Of Integer):
'Initialise the list
Dim myValues As New System.Collections.Generic.List(Of Integer)
'Shortcut to pre-populate it with known values
myValues.AddRange(New Integer() {55, 56, 57})
'Add a new value, dynamically resizing the array
myValues.Add(32)
'It probably has a method do do what you want, but if you really need an array:
myValues.ToArray()
回答by Eric Robinson
you add the number to
你把号码加到
holdValues(x) //x+1 will be size of array
so something like this
所以像这样
Dim array(2) As Integer
array(0) = 100
array(1) = 10
array(2) = 1
you can re-allocate the array to be bigger if needed by doing this.
如果需要,您可以通过执行此操作将数组重新分配为更大。
ReDim array(10) as Integer
you'll have to add in your code when you should make your array bigger. You can also look into lists. Lists take care of this issue automatically.
当你应该让你的数组更大时,你必须添加你的代码。您还可以查看列表。列表会自动处理这个问题。
here's some info on Lists: http://www.dotnetperls.com/list-vbnet
这里有一些关于列表的信息:http: //www.dotnetperls.com/list-vbnet
Hope this helps.
希望这可以帮助。
Also a link for general knowledge on arrays http://www.dotnetperls.com/array-vbnet
还有一个关于数组的一般知识的链接http://www.dotnetperls.com/array-vbnet