Jagged Arrays in Visual Basic.NET

To get to this tutorial on Jagged Arrays in Visual Basic, the previous tutorials on Arrays introduced the concepts of one dimensional arrays, two dimensional arrays and three dimensional arrays.

To recap, a three-dimensional array is an array of arrays as illustrated below

3 dimensional array

In that tutorial, my friend Harry and I had 3 books each on Visual Basic and I stored these two arrays in a three dimensional array.

What would happen however if I had 8 books and Harry had 3?   We could use a three dimensional array, however we would have 5 empty wasted rows for Harry.  This is inefficient.  It also makes coding that bit harder as we don’t know the true upper bound of any of the dimensions in the array.

To solve this problem, Microsoft introduced Jagged Arrays in Visual Basic .NET.  A jagged array is an array of arrays, but each array may be of uneven size.

Look at the following two arrays of mine and Harry’s books. I have two books but Harry, being the bookworm that he is, has four.

Create a new project called JaggedArray.  Add a ListBox to Form1 called ListBoxBooks

Add the following code to the Form, which creates two one dimensional arrays; myBooks and harrysBooks

'My books
Dim myBooks(1) As String

myBooks (0) = “Microsoft Visual Basic 2010 Step By Step”
myBooks (1) = “Sams Teach Yourself Visual Basic 2012 in 24 Hours”

'Harry’s books
Dim harrysBooks(3) As String
harrysBooks (0) = “Excel VBA Programming For Dummies”
harrysBooks (1) = “Sams Teach Yourself Visual Basic 2012 in 24 Hours”
harrysBooks (2) = “Learn to Program with Visual Basic”
harrysBooks (3) = “Distributed Applications with Microsoft Visual Basic 6.0”

The syntax to declare a jagged array to store these two uneven arrays is

Dim jaggedArray()() As String = New String(1)() {}

Note the use of two brackets in the declaration on the left hand side.  This informs Visual Basic to allocate space for a jagged array.  The right hand side instantiates a new string array, the first bracket (1) informs Visual Basic that the jagged array will contain two arrays.  The second empty parenthesis informs Visual Basic that the arrays will be one dimensional.  The {} is required and can be used to populate the array there and then.  However we already have two arrays, to populate the jagged array with the arrays

jaggedArray(0) = myBooks
jaggedArray(1) = harrysBooks

To iterate this array

'Loop through each jagged array
For i As Integer = 0 To jaggedArray.GetUpperBound(0)

     'loop through the rows in the arrays
      For j As Integer = 0 To jaggedArray(i).GetUpperBound(0)
           ListBoxBooks.Items.Add(jaggedArray(i)(j))
      Next
Next

In the code above I am first iterating through the arrays in the jagged array, and I am then iterating through each row of these arrays.

The jagged array is storing both arrays of uneven size in a more efficient manner than a multi-dimension array.  There are no wasted rows.

Say I wanted to add the column Author to the arrays, I would declare the arrays as follows

'My books
Dim myBooks(1, 1) As String
myBooks(0, 0) = "Microsoft Visual Basic 2010 Step By Step"
myBooks(0, 1) = "Michael Halvorson"
myBooks(1, 0) = "Sams Teach Yourself Visual Basic 2012 in 24 Hours"
myBooks(1, 1) = "James Foxal"

'Harry’s books
Dim harrysBooks(3, 1) As String
harrysBooks(0, 0) = "Excel VBA Programming For Dummies"
harrysBooks(0, 1) = "John Walkenbach"
harrysBooks(1, 0) = "Sams Teach Yourself Visual Basic 2012 in 24 Hours"
harrysBooks(1, 1) = "James Foxal"
harrysBooks(2, 0) = "Learn to Program with Visual Basic"
harrysBooks(2, 1) = "John Smiley"
harrysBooks(3, 0) = "Distributed Applications with Microsoft Visual Basic 6.0"
harrysBooks(3, 1) = "Microsoft"

The syntax to create a jagged array capable of storing these two dimensional arrays

Dim jaggedArray()(,) As String = New String(1)(,) {}

The one minor difference to the first example is the comma in the second parenthesis.  This indicates to Visual Basic that the two arrays will contain two dimensions.

To populate this jagged array with the two arrays

jaggedArray(0) = myBooks
jaggedArray(1) = harrysBooks

And to iterate the contents of these two arrays

'loop through the arrays stored in the jagged array
For i As Integer = 0 To jaggedArray.GetUpperBound(0)

     'loop through the rows in the arrays
     For j As Integer = 0 To jaggedArray(i).GetUpperBound(0)

          'loop the columns
          For k = 0 To jaggedArray(i).GetUpperBound(1)

               ListBoxBooks.Items.Add(jaggedArray(i)(j, k))
          Next
     Next
Next

This iteration code is very robust.  I could add extra columns to either array and this code will continue to function.  There is some code prior to this loop that isn’t so robust.  Can you spot it?

There isn’t actually much material on multi-dimensional jagged arrays out there but they are a very efficient and relatively simple means to store uneven data structures.  Understanding these concepts puts you ahead of many, and I mean many, seasoned professionals.

Well done.