2D, 3D and Jagged Array ExamplesUse 2D arrays along with 3D and jagged arrays. Review the syntax for these arrays.
VB.NET
2D arrays. 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. 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.
Array
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.
Step 1 This program populates a new 2D array. The "," syntax is used to declare the array as a two-dimensional array.
Step 2 We call GetUpperBound—this returns how many elements are in a dimension (bound) of the array.
Step 3 We loop over the elements in the 2D array, and write them to the screen with Console.Write.
Console 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 Module
AA BB CC DD
Three-dimensional. Arrays sometimes have more than one or two dimensions. Three-dimensional arrays are rarely useful. But the syntax is easy to understand.
Detail 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.
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
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.
Info 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.
For Each, For
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
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.