Take Advantage of Jagged Arrays
.NET supports arrays of arrays, or "jagged arrays." Here's when they come in handy.
by Francesco Balena
VSLive! Orlando, September 18, 2002
Note: Francesco Balena led the pre-conference workshop, "VB.NET2TheMax" at VSLive! Orlando, Sunday, September 15. This tip is from that session.
Many developers overlook the fact that Visual Basic .NET supports arrays of arrays, that is, arrays whose elements are arrays. This is a familiar concept to most C++ programmers, but it might be new to many Visual Basic programmers. To tell the truth, you can create such structures in previous versions of the language by storing arrays in Variant variants, but the approach isn't as clean as in VB.NET, which supports arrays of arrays natively, so you don't have to resort to any hack.
Arrays of arraysalso known as jagged arraysare especially useful when you have a two-dimensional matrix whose rows don't have the same length. You can render this structure by using a standard two-dimensional array, but you'd have to size it to accommodate the row with the highest number of elements, which would waste space. The arrays-of-arrays concept isn't limited to two dimensions only, and you might need three-dimensional or four-dimensional jagged arrays. Here is an example of a "triangular" matrix of strings:
"a00"
"a10" "a11"
"a20" "a21" "a22"
"a30" "a31" "a32" "a33"
Even though Visual Basic .NET supports arrays of arrays natively, I can't consider their syntax to be intuitive. This code snippet shows how you can initialize the preceding structure and then process it by expanding its rows:
Sub TestJaggedArray()
' Initialize an array of arrays.
Dim arr()() As String = {New String() {"a00"}, _
New String() {"a10", "a11"}, _
New String() {"a20", "a21", "a22"}, _
New String() {"a30", "a31", "a32", "a33"}}
' Show how you can reference an element.
Console.WriteLine(arr(3)(1))
' => a31
' Assign an entire row.
arr(0) = New String() {"a00", "a01", "a02"}
' Read an element just added.
Console.WriteLine(arr(0)(2))
' => a02
' Expand one of the rows.
ReDim Preserve arr(1)(3)
' Assign the new elements.
' (Currently they are Nothing.)
arr(1)(2) = "a12"
arr(1)(3) = "a13"
' Read back one of them.
Console.WriteLine(arr(1)(2))
' => a12
End Sub
Jagged arrays have another, non-obvious advantage over standard two-dimensional arrays: Each row of a jagged array can be processed as if it were an independent array. For example, you can sort, search, and reverse individual rows using shared methods of the Array class, whereas performing the same operation on a standard two-dimensional array would require a loop or some other custom code. This code snippet sorts in descending order the elements of the first row of the array built previously, and then displays its elements:
' sort the first row in descending order
Array.Sort(arr(0))
Array.Reverse(arr(0))
' display the first row
Dim s As String
For Each s In arr(0)
Console.WriteLine(s)
Next
About the Author
Francesco Balena is the editor in chief of Visual Basic Journal, VSM's Italian licensee; the author of Programming Microsoft Visual Basic 6.0 and Programming Microsoft Visual Basic .NET (Microsoft Press); and a regular speaker at VSLive! and other developer conferences. He teaches VB.NET classes for Wintellect and is the founder of the www.vb2themax.com Web site.
|