Instantiation and accessing the elements of multidimensional arrays
Recall that we instantiate our single dimensional arrays like this:
Dim intArray(4) As Integer
The above line would instantiate (create) a one dimensional array (
intArray) of type Integer whose length would be 5. We can access the elements of the array like this:
intArray(0) = 45 ' set the first element to 45
intArray(2) = 21 ' set the third element to 21
intArray(4) = 9 ' set the fifth and last element to 9
Instantiating a multidimensional array is almost identical to the above procedure as long as you keep the most basic definition of the multidimensional array in mind which is 'a multidimensional array is an array of arrays'. Suppose we wish to create a two dimensional rectangular array with 2 rows and 3 columns. We can instantiate the array as follows
Dim myTable(1, 2) As Integer
All the elements of the array are auto-initialized to their default values; hence all the elements of our
myTable array would be initialized with zero. We can iterate through this array using either a
For Each or a
For...Next loop.
Dim intVal As Integer
For Each intVal In myTable
Console.WriteLine(intVal)
Next
When it is compiled and executed, it will print six (2 x 3) zeros at the console.
0
0
0
0
0
0
Let's change the values of the individual elements of the array. To change the value of the first element of the first row to 32, we can use the following code
myTable(0,0) = 32
In the same way, we can change the values of other elements in the array
myTable(0,1) = 2
myTable(0,2) = 12
myTable(1,0) = 18
myTable(1,1) = 74
myTable(1,2) = -13
We can use a for loop to iterate this array
Dim row As Integer
Dim col As Integer
For row = 0 To myTable.GetLength(0) - 1
For col = 0 To myTable.GetLength(1) - 1
Console.WriteLine("Element at ({0},{1}) is {2}", row, col, myTable(row, col))
Next
Next
Above, we have used two
For...Next loops to iterate through each of the two dimensions of the array. We have used the
GetLength() method of the
System.Array class (the underlying class for arrays in .Net) to find the length of a particular dimension of the array. Note that the
Length property will give a total number of elements within this two dimensional array, i.e., 6. The output of the above program will be
Element at (0,0) is 32
Element at (0,1) is 2
Element at (0,2) is 12
Element at (1,0) is 18
Element at (1,1) is 74
Element at (1,2) is -13
Press any key to continue
Instantiating and accessing a Jagged Array
A jagged array is an array in which the length of each row is not the same. For example we may wish to create a table with 3 rows where the length of first row is 3, the second row is 5 and the third row is 2. We can instantiate this jagged array as
Dim myTable(2)() As Integer
myTable(0) = New Integer(2) {}
myTable(1) = New Integer(4) {}
myTable(2) = New Integer(1) {}
Then we can fill the array as
myTable(0)(0) = 3
myTable(0)(1) = -2
myTable(0)(2) = 16
myTable(1)(0) = 1
myTable(1)(1) = 9
myTable(1)(2) = 5
myTable(1)(3) = 6
myTable(1)(4) = 98
myTable(2)(0) = 19
myTable(2)(1) = 6
We will show you how to use the
For Each loop to access the elements of the array
Dim row() As Integer
Dim col As Integer
For Each row In myTable
For Each col In row
Console.WriteLine(col)
Next
Console.WriteLine()
Next
The code above is very simple and easily understandable. We picked up each
row (which is an
Integer array) and then iterated through the
row while printing each of its columns. The output of the above code will be
3
-2
16
1
9
5
6
98
19
6
Press any key to continue
In the same way, we can use a three-dimensional array as
Dim myTable(2, 1, 3) As Integer
myTable(0, 0, 0) = 3
myTable(1, 1, 1) = 6
Or in the jagged array fashion as
Dim myTable(1)()() As Integer
myTable(0) = New Integer(1)() {}
myTable(0)(0) = New Integer(2) {}
myTable(0)(1) = New Integer(3) {}
myTable(1) = New Integer(2)() {}
myTable(1)(0) = New Integer(1) {}
myTable(1)(1) = New Integer(3) {}
myTable(1)(2) = New Integer(2) {}
myTable(0)(0)(0) = 34
myTable(0)(1)(1) = 43
myTable(1)(2)(2) = 76
We have created a three dimensional jagged array. It is an array of two 2-dimensional arrays. The first of the 2-dimensional arrays contain 2 rows. The length of the first row is 3 while the length of the second row is 4. In the similar fashion, the second two dimensional array is also initialized. In the end, we accessed some of the elements of the array and assigned them different values. Although the higher dimensional jagged arrays are quite difficult to perceive; they may be very useful in certain complex problems. Again, the key to avoid confusion with regards multidimensional arrays is to perceive them as 'an array of arrays'.