HomeSearch

VB.NET 2D, 3D and Jagged Array Examples

Use 2D arrays along with 3D and jagged arrays. Review the syntax for these arrays.
2D arrays. Consider a chessboard. A knight moves in a certain way, a queen in another. The board is a grid of squares: it is a 2D array and can be modeled with a 2D array.
Other arrays. In the VB.NET language we have more options than just a 2D array. We have 3D arrays (which are not that useful usually) and jagged arrays too.
Two-dimensional. 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.

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
Three-dimensional. Arrays sometimes have more than one or two dimensions. Three-dimensional arrays are rarely useful. But the syntax is easy to understand.

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
Jagged array. 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.

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
Jagged, performance. Jagged arrays can use less memory and be faster than two-dimensional arrays. If the shape of your data is uneven, they can save a lot of memory.
Jagged, continued. Jagged arrays can be slower to create than 2D arrays. They require multiple allocations. They can use some optimizations meant for 1D arrays that 2D arrays don't use.
A review. 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.Array
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.