课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 数组

当前位置:免费教程 » 程序设计 » VB.Net
数组存储相同类型的元素的固定大小顺序集合。 数组用于存储数据集合,但将数组视为同一类型的变量的集合通常更有用。

所有数组由连续的内存位置组成。 最低地址对应于第一个元素,最高地址对应于最后一个元素。


在VB.Net数组

在VB.Net中创建数组

要在VB.Net中声明数组,可以使用Dim语句。 例如,

  1. Dim intData(30) ' an array of 31 elements
  2. Dim strData(20) As String ' an array of 21 strings
  3. Dim twoDarray(10, 20) As Integer 'a two dimensional array of integers
  4. Dim ranges(10, 100) 'a two dimensional array


您还可以在声明数组时初始化数组元素。 例如,

  1. Dim intData() As Integer = {12, 16, 20, 24, 28, 32}
  2. Dim names() As String = {"Karthik", "Sandhya", _
  3. "Shivangi", "Ashwitha", "Somnath"}
  4. Dim miscData() As Object = {"Hello World", 12d, 16ui, "A"c}


可以通过使用数组的索引来存储和访问数组中的元素。 以下程序演示了这一点:

  1. Module arrayApl
  2. Sub Main()
  3. Dim n(10) As Integer ' n is an array of 11 integers '
  4. Dim i, j As Integer
  5. ' initialize elements of array n '
  6. For i = 0 To 10
  7. n(i) = i + 100 ' set element at location i to i + 100
  8. Next i
  9. ' output each array element's value '
  10. For j = 0 To 10
  11. Console.WriteLine("Element({0}) = {1}", j, n(j))
  12. Next j
  13. Console.ReadKey()
  14. End Sub
  15. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Element(0) = 100
  2. Element(1) = 101
  3. Element(2) = 102
  4. Element(3) = 103
  5. Element(4) = 104
  6. Element(5) = 105
  7. Element(6) = 106
  8. Element(7) = 107
  9. Element(8) = 108
  10. Element(9) = 109
  11. Element(10) = 110


动态数组

动态数组是可以根据程序需要进行维度和重新定义的数组。 您可以使用ReDim语句声明一个动态数组。

ReDim语句的语法:

  1. ReDim [Preserve] arrayname(subscripts)
  • Preserve关键字有助于在调整现有数组大小时保留现有数组中的数据。

  • arrayname是要重新维度的数组的名称。

  • subscripts指定新维度。

  1. Module arrayApl
  2. Sub Main()
  3. Dim marks() As Integer
  4. ReDim marks(2)
  5. marks(0) = 85
  6. marks(1) = 75
  7. marks(2) = 90
  8. ReDim Preserve marks(10)
  9. marks(3) = 80
  10. marks(4) = 76
  11. marks(5) = 92
  12. marks(6) = 99
  13. marks(7) = 79
  14. marks(8) = 75
  15. For i = 0 To 10
  16. Console.WriteLine(i & vbTab & marks(i))
  17. Next i
  18. Console.ReadKey()
  19. End Sub
  20. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. 0 85
  2. 1 75
  3. 2 90
  4. 3 80
  5. 4 76
  6. 5 92
  7. 6 99
  8. 7 79
  9. 8 75
  10. 9 0
  11. 10 0


多维数组

VB.Net允许多维数组。多维数组也被称为矩形数组。


你可以声明一个二维的字符串数组:

  1. Dim twoDStringArray(10, 20) As String


或者,整数变量的3维数组:

  1. Dim threeDIntArray(10, 10, 10) As Integer


下面的程序演示创建和使用二维数组:

  1. Module arrayApl
  2. Sub Main()
  3. ' an array with 5 rows and 2 columns
  4. Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
  5. Dim i, j As Integer
  6. ' output each array element's value '
  7. For i = 0 To 4
  8. For j = 0 To 1
  9. Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i, j))
  10. Next j
  11. Next i
  12. Console.ReadKey()
  13. End Sub
  14. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. a[0,0]: 0
  2. a[0,1]: 0
  3. a[1,0]: 1
  4. a[1,1]: 2
  5. a[2,0]: 2
  6. a[2,1]: 4
  7. a[3,0]: 3
  8. a[3,1]: 6
  9. a[4,0]: 4
  10. a[4,1]: 8


不规则数组

Jagged数组是一个数组的数组。 以下代码显示了声明一个名为score of Integers的不规则数组:

  1. Dim scores As Integer()() = New Integer(5)(){}


下面的例子说明使用不规则数组:

  1. Module arrayApl
  2. Sub Main()
  3. 'a jagged array of 5 array of integers
  4. Dim a As Integer()() = New Integer(4)() {}
  5. a(0) = New Integer() {0, 0}
  6. a(1) = New Integer() {1, 2}
  7. a(2) = New Integer() {2, 4}
  8. a(3) = New Integer() {3, 6}
  9. a(4) = New Integer() {4, 8}
  10. Dim i, j As Integer
  11. ' output each array element's value
  12. For i = 0 To 4
  13. For j = 0 To 1
  14. Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i)(j))
  15. Next j
  16. Next i
  17. Console.ReadKey()
  18. End Sub
  19. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. a[0][0]: 0
  2. a[0][1]: 0
  3. a[1][0]: 1
  4. a[1][1]: 2
  5. a[2][0]: 2
  6. a[2][1]: 4
  7. a[3][0]: 3
  8. a[3][1]: 6
  9. a[4][0]: 4
  10. a[4][1]: 8


Array类

Array类是VB.Net中所有数组的基类。 它在系统命名空间中定义。 Array类提供了处理数组的各种属性和方法。


Array类的属性

下表提供了一些Array类中最常用的属性

SN属性名称和说明
1

IsFixedSize

Gets a value indicating whether the Array has a fixed size.

获取一个值,指示数组是否具有固定大小。

2

IsReadOnly

Gets a value indicating whether the Array is read-only.

获取一个值,该值指示Array是否为只读。

3

Length

Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.

获取一个32位整数,表示数组所有维度中元素的总数。

4

LongLength

Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array.

获取一个64位整数,表示数组所有维度中元素的总数。

5

Rank

Gets the rank (number of dimensions) of the Array.

获取数组的排名(维数)。


Array类的方法

下表提供了一些最常用的Array类方法:

SN方法名称和说明
1

Public Shared Sub Clear (array As Array, index As Integer, length As Integer)
公共共享子清除(数组为数组,指数为整数,长度为整数)

设置一个范围的数组元素的零,为false,或为空,这取决于元素类型。

2

Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)
公共共享子复制(sourceArray作为数组,destinationArray作为数组,长度为整数)

复制一定范围内由数组以第一个元素的元素,并将它们粘贴到起始于第一个元素另一个数组。长度被指定为32位整数。

3

Public Sub CopyTo (array As Array, index As Integer)
公共Sub CopyTo(数组为数组,指数为整数)

将当前的一维数组到指定的一维数组从指定的目标数组索引处的所有元素。索引被指定为32位整数。

4

Public Function GetLength (dimension As Integer) As Integer
公共功能对GetLength(尺寸为整数)作为整数

获取一个32位整数,它表示数组的指定维中的元素的数量。

5

Public Function GetLongLength (dimension As Integer) As Long
公共职能GetLongLength(尺寸为整数),只要

获取一个64位整数,它代表了数组的指定维中的元素的数量。

6

Public Function GetLowerBound (dimension As Integer) As Integer
公共职能GetLowerBound(尺寸为整数)作为整数

获取下界在数组中指定的尺寸。

7

Public Function GetType As Type
公共职能的GetType为类型

获取当前实例的类型(从Object继承)。

8

Public Function GetUpperBound (dimension As Integer) As Integer
公共职能GetUpperBound(尺寸为整数)作为整数

获取上限在数组中指定的尺寸。

9

Public Function GetValue (index As Integer) As Object
公共职能的GetValue(指数为整数)作为对象

获取在一维数组中指定位置的值。索引被指定为32位整数。

10

Public Shared Function IndexOf (array As Array,value As Object) As Integer
公共共享功能的IndexOf(数组作为数组,值作为对象)作为整数

搜索指定的对象,并返回第一次出现的整个一维数组中的索引。

11

Public Shared Sub Reverse (array As Array)
公共共享子反向(阵列阵列)

反转在整个一维数组中的元素的顺序。

12

Public Sub SetValue (value As Object, index As Integer)
公用Sub的SetValue(价值为对象,指数为整数)

在一维阵列中的指定位置设置一个值的元素。索引被指定为32位整数。

13

Public Shared Sub Sort (array As Array)
公共共享子排序(数组为数组)

使用排序了IComparable实现阵列中的每个元素在整个一维数组中的元素。

14

Public Overridable Function ToString As String
公众可重写的ToString函数作为字符串

返回表示当前对象(从Object继承)的字符串。

有关Array类属性和方法的完整列表,请参阅Microsoft文档。


示例

下面的程序演示使用的一些Array类的方法:

  1. Module arrayApl
  2. Sub Main()
  3. Dim list As Integer() = {34, 72, 13, 44, 25, 30, 10}
  4. Dim temp As Integer() = list
  5. Dim i As Integer
  6. Console.Write("Original Array: ")
  7. For Each i In list
  8. Console.Write("{0} ", i)
  9. Next i
  10. Console.WriteLine()
  11. ' reverse the array
  12. Array.Reverse(temp)
  13. Console.Write("Reversed Array: ")
  14. For Each i In temp
  15. Console.Write("{0} ", i)
  16. Next i
  17. Console.WriteLine()
  18. 'sort the array
  19. Array.Sort(list)
  20. Console.Write("Sorted Array: ")
  21. For Each i In list
  22. Console.Write("{0} ", i)
  23. Next i
  24. Console.WriteLine()
  25. Console.ReadKey()
  26. End Sub
  27. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Original Array: 34 72 13 44 25 30 10
  2. Reversed Array: 10 30 25 44 13 72 34
  3. Sorted Array: 10 13 25 30 34 44 72
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号