C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: This program populates a new 2D array. The (,) syntax is used to declare the array as a two-dimensional array.
And: For initializing the values, we use the String constructor and then the {} brackets to contain the element rows.
GetUpperBound: Returns how many elements are in a dimension (bound) of the array. For a 2D array, we can use the arguments 0 or 1.
VB.NET program that uses 2D array
Module Module1
Sub Main()
' Declare two-dimensional array of strings.
Dim values(,) As String =
New String(,) {{"ant", "aunt"},
{"Sam", "Samantha"},
{"clozapine", "quetiapine"},
{"flomax", "volmax"},
{"toradol", "tramadol"}}
' Get bounds of the array.
Dim bound0 As Integer = values.GetUpperBound(0)
Dim bound1 As Integer = values.GetUpperBound(1)
' 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 Module
Output
ant aunt
Sam Samantha
clozapine quetiapine
flomax volmax
toradol tramadol
First: When you create a 3D array, please specify the dimensions of the array with the maximum indexes of each dimension.
Next: To assign elements within the multidimensional array we use three numbers separated by commas.
Tip: The argument to GetLength is the dimension index. GetLength returns an element count, not a maximum index of the dimension.
VB.NET program that creates 3D array
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 Module
Output
100
200
300
400
500
000
020
000
000
000
000
000
003
000
000
000
000
004
000
000
First: This program uses the ()() syntax after the jagged array identifier to signify that the array contains other arrays.
Also: Temporary arrays are created in the standard way in VB.NET and then assigned to elements in the jagged array.
Finally: Two nested For-loops are used to loop through the top-level array and all the subarrays.
VB.NET program that uses jagged array
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 Module
Output
1 2 3
0
3 4 5 6