# [学习记录]VB 数组的应用

1.

``Dim Waiters () As String = {"Ben","Sue","Lee","Kim","Pat"}``

2.

``Dim Investments() = {500,600,700,23.5,188.9}``

3.

``Dim Rectangle = {{10，20}，{50，60}，{23，15}}``

4.但是如果这样定义：

``Dim arr1() = {{1, 2, 3}, {7, 2, 3}, {0, 2, 1}}``

``Dim arr1() = {({1, 2, 3}), ({7, 2, 3}), ({0, 2, 1})}``

5.如果这样做

``````        Dim arr1() As Single
arr1(1) = 30``````

``````        Dim arr1() As Single = {0, 0, 0}
arr1(1) = 30
arr1(0) = 10``````

6.如果要增加上面arr1数组的容量，并清掉原来数据，变为{0，0，0，0}，就需要用Redim

``````        Dim arr1() As Single = {0, 0, 0}
arr1(1) = 30
arr1(0) = 10
ReDim arr1(3)``````

7.但是如果要保留原来的数据只是末尾增加容量，就需要用Redim Preserve

``````        Dim arr1() As Single = {0, 0, 0}
arr1(1) = 30
arr1(0) = 10
ReDim Preserve arr1(3)``````

8.

``````        Dim arr1(,,) As Single
Txtnote.Text = arr1(0, 0, 0) & arr1(0, 1, 0)``````

``````        Dim arr1(,,) As Single
arr1(0, 0, 0) = 23``````

``````        Dim arr1(,,) As Single
ReDim arr1(25, 25, 25)
arr1(10, 1, 1) = 150``````

9.Redim 必须配合Dim一起用

10如果要改变多维数组内容并保持之前内容，则可以用Redim Preserve,但只能更改最后维的数组的大小，比如前面的arr1(25,25,25),就只能Redim第三维,如下第三维被从25扩展到50

``````Dim arr1(,,) As Single
Redim arr1(25,25,25)
arr1(10,1,1)=150
Redim Preserve arr1(25,25,50)``````

...全文
72 回复 打赏 收藏 举报

7617

VB 基础类

2022-05-10 14:53