Array OOP
Array OOP
An array is used
to store a collection of data, but it is often more useful to think of an array as a collection of
variables of the same type.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element
You can also initialize the array elements while declaring the array. For example,
The elements in an array can be stored and accessed by using the index of the array. The
following program demonstrates this −
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
When the above code is compiled and executed, it produces the following result −
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
Dynamic Arrays
Dynamic arrays are arrays that can be dimensioned and re-dimensioned as par the need of the
program. You can declare a dynamic array using the ReDim statement.
Where,
The Preserve keyword helps to preserve the data in an existing array, when you resize it.
arrayname is the name of the array to re-dimension.
subscripts specifies the new dimension.
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
When the above code is compiled and executed, it produces the following result −
0 85
1 75
2 90
3 80
4 76
5 92
6 99
7 79
8 75
9 0
10 0
Multi-Dimensional Arrays
VB.Net allows multidimensional arrays. Multidimensional arrays are also called rectangular
arrays.
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
When the above code is compiled and executed, it produces the following result −
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
Jagged Array
A Jagged array is an array of arrays. The following code shows declaring a jagged array
named scores of Integers −
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
When the above code is compiled and executed, it produces the following result −
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
The following table provides some of the most commonly used properties of the Array class −
3 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.
Public Function GetLength (dimension As Integer) As Integer
4
Gets a 32-bit integer that represents the number of elements in the specified dimension of
the Array.
Public Function GetLongLength (dimension As Integer) As Long
5
Gets a 64-bit integer that represents the number of elements in the specified dimension of
the Array.
Public Function GetLowerBound (dimension As Integer) As Integer
6
Gets the lower bound of the specified dimension in the Array.
Public Function GetType As Type
7
Gets the Type of the current instance (Inherited from Object).
Public Function GetUpperBound (dimension As Integer) As Integer
8
Gets the upper bound of the specified dimension in the Array.
Public Function GetValue (index As Integer) As Object
9
Gets the value at the specified position in the one-dimensional Array. The index is
specified as a 32-bit integer.
Public Shared Function IndexOf (array As Array,value As Object) As Integer
10
Searches for the specified object and returns the index of the first occurrence within the
entire one-dimensional Array.
Public Shared Sub Reverse (array As Array)
11
Reverses the sequence of the elements in the entire one-dimensional Array.
Public Sub SetValue (value As Object, index As Integer)
12
Sets a value to the element at the specified position in the one-dimensional Array. The
index is specified as a 32-bit integer.
Public Shared Sub Sort (array As Array)
13
Sorts the elements in an entire one-dimensional Array using the IComparable
implementation of each element of the Array.
Public Overridable Function ToString As String
14
Returns a string that represents the current object (Inherited from Object).
For complete list of Array class properties and methods, please consult Microsoft documentation.
Example
The following program demonstrates use of some of the methods of the Array class:
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: ")
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