Sometimes data is part of a two-dimensional space. We can use VB.NET to represent this space—a 2D array can be allocated and used.
In this language we have more options than just a 2D array. We have 3D arrays (which are not that useful usually) and jagged arrays too.
2D arrays have complex syntax. We have data that should be stored in rows and columns. With a two-dimensional array, we store a rectangular collection of elements.
GetUpperBound
—this returns how many elements are in a dimension (bound) of the array.Console.Write
.Module Module1 Sub Main() ' Step 1: declare two-dimensional array of strings. Dim values(,) As String = New String(,) {{"AA", "BB"}, {"CC", "DD"}} ' Step 2: get bounds of the array. Dim bound0 As Integer = values.GetUpperBound(0) Dim bound1 As Integer = values.GetUpperBound(1) ' Step 3: loop over all elements. For i As Integer = 0 To bound0 For x As Integer = 0 To bound1 ' Get element. Dim s1 As String = values(i, x) Console.Write(s1) Console.Write(" "c) Next Console.WriteLine() Next End Sub End ModuleAA BB CC DD
Arrays sometimes have more than one or two dimensions. Three-dimensional arrays are rarely useful. But the syntax is easy to understand.
GetLength
is the dimension index. GetLength
returns an element count, not a maximum index of the dimension.Module Module1 Sub Main() ' Declare 3D array. ' ... Specify maximum indexes of dimensions. Dim threeD(2, 4, 3) As Integer threeD(0, 0, 0) = 1 threeD(0, 1, 0) = 2 threeD(0, 2, 0) = 3 threeD(0, 3, 0) = 4 threeD(0, 4, 0) = 5 threeD(1, 1, 1) = 2 threeD(2, 2, 2) = 3 threeD(2, 2, 3) = 4 ' Loop over three dimensions and display. For i As Integer = 0 To threeD.GetLength(2) - 1 For y As Integer = 0 To threeD.GetLength(1) - 1 For x As Integer = 0 To threeD.GetLength(0) - 1 Console.Write(threeD(x, y, i)) Next Console.WriteLine() Next Console.WriteLine() Next End Sub End Module100 200 300 400 500 000 020 000 000 000 000 000 003 000 000 000 000 004 000 000
This kind of array is uneven in shape—it is an array of arrays. If the subarrays you need vary in length, a jagged array becomes extremely efficient.
For
-loops are used to loop through the top-level array and all the subarrays.Module Module1 Sub Main() ' Create jagged array with maximum index of 2. Dim jagged()() As Integer = New Integer(2)() {} ' Create temporary array and place in index 0. Dim temp(2) As Integer temp(0) = 1 temp(1) = 2 temp(2) = 3 jagged(0) = temp ' Create small temporary array and place in index 1. Dim temp2(0) As Integer jagged(1) = temp2 ' Use array constructor and place result in index 2. jagged(2) = New Integer() {3, 4, 5, 6} ' Loop through top-level arrays. For i As Integer = 0 To jagged.Length - 1 ' Loop through elements in subarrays. Dim inner As Integer() = jagged(i) For a As Integer = 0 To inner.Length - 1 Console.Write(inner(a)) Console.Write(" "c) Next Console.WriteLine() Next End Sub End Module1 2 3 0 3 4 5 6
Arrays are powerful things. With them we gain the ability to access a unit of data with an integer, not a name. This allows us to create large and complex data sets and use them.