📜  VB.Net-数组

📅  最后修改于: 2020-11-19 08:53:08             🧑  作者: Mango


数组存储相同类型元素的固定大小的顺序集合。数组用于存储数据的集合,但是将数组视为相同类型的变量的集合通常会更有用。

所有阵列均包含连续的内存位置。最低地址对应于第一个元素,最高地址对应于最后一个元素。

VB.Net中的数组

在VB.Net中创建数组

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

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

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

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

数组中的元素可以使用数组的索引进行存储和访问。以下程序演示了这一点-

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

编译并执行上述代码后,将产生以下结果-

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

动态数组

动态数组是可以根据程序需要确定尺寸和重新定义尺寸的数组。您可以使用ReDim语句声明动态数组。

ReDim语句的语法-

ReDim [Preserve] arrayname(subscripts)

哪里,

  • 调整大小时, Preserve关键字有助于将数据保留在现有阵列中。

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

  • 下标指定新尺寸。

Module arrayApl
   Sub Main()
      Dim marks() As Integer
      ReDim marks(2)
      marks(0) = 85
      marks(1) = 75
      marks(2) = 90
     
      ReDim Preserve marks(10)
      marks(3) = 80
      marks(4) = 76
      marks(5) = 92
      marks(6) = 99
      marks(7) = 79
      marks(8) = 75
      
      For i = 0 To 10
         Console.WriteLine(i & vbTab & marks(i))
      Next i
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

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

多维数组

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

您可以将二维字符串数组声明为-

Dim twoDStringArray(10, 20) As String

或者,一个整数变量的3维数组-

Dim threeDIntArray(10, 10, 10) As Integer

以下程序演示了如何创建和使用二维数组-

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

编译并执行上述代码后,将产生以下结果-

a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[4,1]: 8

锯齿状阵列

锯齿状数组是数组的数组。以下代码显示声明一个锯齿状数组,其名称为Scores of Integers-

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

以下示例说明了使用锯齿状数组-

Module arrayApl
   Sub Main()
      'a jagged array of 5 array of integers
      Dim a As Integer()() = New Integer(4)() {}
      a(0) = New Integer() {0, 0}
      a(1) = New Integer() {1, 2}
      a(2) = New Integer() {2, 4}
      a(3) = New Integer() {3, 6}
      a(4) = New Integer() {4, 8}
      Dim i, j As Integer
      ' output each array element's value 
      
      For i = 0 To 4
         For j = 0 To 1
            Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i)(j))
         Next j
      Next i
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8

数组类

Array类是VB.Net中所有数组的基类。它在系统名称空间中定义。 Array类提供了与数组一起使用的各种属性和方法。

数组类的属性

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

Sr.No Property Name & Description
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.

3

Length

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

4

LongLength

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

5

Rank

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

数组类的方法

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

Sr.No Method Name & Description
1

Public Shared Sub Clear (array As Array, index As Integer, length As Integer)

Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.

2

Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)

Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.

3

Public Sub CopyTo (array As Array, index As Integer)

Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.

4

Public Function GetLength (dimension As Integer) As Integer

Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.

5

Public Function GetLongLength (dimension As Integer) As Long

Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array.

6

Public Function GetLowerBound (dimension As Integer) As Integer

Gets the lower bound of the specified dimension in the Array.

7

Public Function GetType As Type

Gets the Type of the current instance (Inherited from Object).

8

Public Function GetUpperBound (dimension As Integer) As Integer

Gets the upper bound of the specified dimension in the Array.

9

Public Function GetValue (index As Integer) As Object

Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.

10

Public Shared Function IndexOf (array As Array,value As Object) As Integer

Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.

11

Public Shared Sub Reverse (array As Array)

Reverses the sequence of the elements in the entire one-dimensional Array.

12

Public Sub SetValue (value As Object, index As Integer)

Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.

13

Public Shared Sub Sort (array As Array)

Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.

14

Public Overridable Function ToString As String

Returns a string that represents the current object (Inherited from Object).

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

以下程序演示了Array类的某些方法的使用:

Module arrayApl
   Sub Main()
      Dim list As Integer() = {34, 72, 13, 44, 25, 30, 10}
      Dim temp As Integer() = list
      Dim i As Integer
      Console.Write("Original Array: ")
      
      For Each i In list
         Console.Write("{0} ", i)
      Next i
      Console.WriteLine()
      ' reverse the array
      Array.Reverse(temp)
      Console.Write("Reversed Array: ")
      
      For Each i In temp
         Console.Write("{0} ", i)
      Next i
      Console.WriteLine()
      'sort the array
      Array.Sort(list)
      Console.Write("Sorted Array: ")
      
      For Each i In list
         Console.Write("{0} ", i)
      Next i
      Console.WriteLine()
      Console.ReadKey()
   End Sub
End Module

编译并执行上述代码后,将产生以下结果-

Original Array: 34 72 13 44 25 30 10
Reversed Array: 10 30 25 44 13 72 34
Sorted Array: 10 13 25 30 34 44 72