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 arraystwo dimensional arrays and three dimensional arrays.  I highly recommend that you read these tutorials if you don’t fully understand the concepts of a 1D or 2D array in Visual Basic.  We’ll be covering some reasonably advanced topics here and grounding in more traditional arrays is needed before the benefits of jagged arrays is really understood.  To recap however, 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 Visual Basic books each and we stored these 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.

Let’s create a Jagged Array of uneven arrays for ourselves.  Create a new project called JaggedArray.  Add a ListBox to Form1 called ListBoxBooks.  Add the following code to the Form Load Event of Form1.    In this code, I have two books but Harry, being the bookworm that he is, has four.

'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 statement New String (1)(){} creates 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 data to populate these arrays, in the form of my books and Harry’s books.

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

To iterate this array add the following code to your Form:

'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, the first loop is iterating through the 2 arrays in the Jagged Array.  The second loop iterates through each row of these arrays.

The Jagged Array is storing the arrays of uneven size in one array.  In a traditional array you can’t have two arrays of uneven size.  They have to contain the same number of columns and rows.  Jagged Arrays are more efficient than a traditional than a multi-dimensional array.  There are no wasted rows.

If 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 arrays is:

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 again use the following syntax:

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

The first loop iterates the arrays in the Jagged Array.  The second loop iterates the rows in each array and the third loop iterates the columns.  This iteration code is very robust.  I could add extra columns to the arrays and the code would continue to function.   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 this concept has put you ahead of many, and I mean many, seasoned professionals.

Congratulations!