VB.NET-整数数组需要实例化,如何实例化



首次尝试

Dim holdValues() As Integer 'Doesn't Work
holdValues(1) = 55

第二次尝试

Dim holdValues(-1) As Integer 'Gives me Index was outside the bounds of the array.
holdValues(1) = 55

我正在尝试做一些类似的事情

 Dim myString(-1) As String

但显然这不适用于整数数组。我不知道数组的大小,它不会变小,但会变大。

任何帮助都将不胜感激,谢谢!

您可以使用Initializers快捷方式:

Dim myValues As Integer() = New Integer() {55, 56, 67}

但是,如果你想调整数组的大小,等等,那么一定要看看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()

您将数字添加到

holdValues(x) //x+1 will be size of array

所以像这个

Dim array(2) As Integer
array(0) = 100
array(1) = 10
array(2) = 1

如果需要,您可以通过这样做将数组重新分配得更大。

ReDim array(10) as Integer 

当您应该增大数组时,您必须添加代码。您也可以查看列表。列表会自动处理此问题。

以下是列表中的一些信息:http://www.dotnetperls.com/list-vbnet

希望这能有所帮助。

也是数组常识的链接http://www.dotnetperls.com/array-vbnet

相关内容

最新更新