1. Computing & Technology

Discuss in my forum

Arrays: The Difference Between Jagged Arrays and Rectangular Arrays

They're really quite different ways to store information!

By , About.com Guide

Rectangular Array

Rectangular Array

In my main Visual Basic tutorial Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial I explain how to use arrays. A reader wrote to ask about initializing a multidimensional rectangular array but he included this code:

 Dim arrTokens(iNumRows)() As String 

This isn't a rectangular array. It's a jagged array. Here's the key difference.

A rectangular array has more than one index in the same array.

 Dim rectArray(D1,D2) As datatype 

There are 18 cells in a 3 by 9 multidimensional array.

A jagged array is an "array of arrays". It's also multidimensional, but each "dimension" doesn't have to have the same number of "elements". Here's the declaration of a jagged array:

 Dim jaggedArray(D1)() As datatype 

A jagged array could be declared with 6 cells in the first row and 5 in the second. The array would have a total of 11 cells. Because Visual Basic doesn't restrict the number of cells in any dimension except the first, VB syntax only allows you to include that dimension in the declaration. (You can leave it out, too.)

In "spreadsheet" terms, you can think of both types of arrays as being a little like a spreadsheet. A two dimensional array is like a spreadsheet of cells. A three dimensional array is all the sheets in a spreadsheet document. A four dimensional array is a collection of spreadsheet documents in a folder and so forth. If you think about it for a moment, it's easy to understand that multidimensional arrays can eat up a lot of computer memory really fast.

It's also possible to declare an array that includes both jagged and multidimensional arrays. This code declares a two dimensional array of arrays.

 Dim jaggedArrray(3, 2)()() As datatype 

Think of a 4 by 3 spreadsheet (.NET arrays start at 0) where each cell contains the address of a jagged array. Lots of other combinations are possible too, but the syntax to use them will give you a headache!

The big advantage of rectangular arrays is that the code is fairly easy to understand. Initializing a 10 by 10 rectangular array is just a nested loop:

 Dim rectArray(9, 9) As String
 Dim i, j As Integer
 For i = 0 To 9
    For j = 0 To 9
       rectArray(i, j) = "George"
    Next
 Next 

Initializing a jagged array is more confusing.

 Dim jaggedArray(9)() As String
 Dim i, j As Integer
 For i = 0 To 9
    jaggedArray(i) = New String(9) {}
    For j = 0 To 9
       jaggedArray(i)(j) = "George"
    Next
 Next 

Since the 'root' of the jagged array is just an array of pointers to other arrays, each one of them has to be declared and initialized too. If the syntax still confuses you, write it out without the loops:

 Dim jaggedArray(9)() As String
 Dim i, j As Integer
 jaggedArray(0) = New String(0) {}
 jaggedArray(1) = New String(1) {}
 jaggedArray(2) = New String(2) {}
 ... etc
 jaggedArray(0)(0) = "George"
 jaggedArray(0)(1) = "George"
 jaggedArray(0)(2) = "George"
 ... etc 

This is what's really happening.

Jagged Arrays With More Than Two Dimensions

I couldn't find even a single reference showing how to work with jagged arrays with more than two dimensions so the explanation below can't be found anywhere else as far as I know. The syntax to work with them is even more confusing, but if you keep the explanation above in mind, it still makes sense.

Suppose your goal is to put the string "Wow" deep inside the third dimension of a jagged array:

 jaggedArrTest(5)(8)(42) = "Wow" 

The first step is to declare the entire jagged array.

 Dim jaggedArrTest(5)()() As String 

Create another jagged array inside the elements of the first in the correct position. And a third inside the second in the correct position.

 jaggedArrTest(5) = New String(8)() {}
 jaggedArrTest(5)(8) = New String(42) {} 

Now your original statement will work.

 jaggedArrTest(5)(8)(42) = "Wow" 

The compiler creates empty cells up to where you wanted to save the string, "Wow". The illustration below shows that in Visual Studio.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

©2012 About.com. All rights reserved.

A part of The New York Times Company.