Multidimensional Jagged Arrays

If you come from a C/C++ or Java background, you’re probably already familiar with jagged arrays, because those languages don’t support rectangular multidimensional arrays like C# does. The only way to implement multidimensional arrays is to create arrays of arrays, which is precisely what a jagged array is. However, because each element of the top-level array is an individual array instance, each array instance in the top-level array can be any size. Therefore, the array isn’t necessarily rectangular—hence, the name jagged arrays.

The syntactical pattern for declaring a jagged array in C# is similar to its cousins C++ and Java. The following example shows how to allocate and use a jagged array:

using System;
using System.Text;
public class EntryPoint
{
static void Main() {
int[][] jagged = new int[3][];
jagged[0] = new int[] {1, 2};
jagged[1] = new int[] {1, 2, 3, 4, 5};
jagged[2] = new int[] {6, 5, 4};
foreach( int[] ar in jagged ) {
StringBuilder sb = new StringBuilder();
foreach( int n in ar ) {
sb.AppendFormat( "{0} ", n );
}
Console.WriteLine( sb.ToString() );
}
Console.WriteLine();
for( int i = 0; i < jagged.Length; ++i ) {
StringBuilder sb = new StringBuilder();
for( int j = 0; j < jagged[i].Length; ++j ) {
sb.AppendFormat( "{0} ", jagged[i][j] );
}
Console.WriteLine( sb.ToString() );
}
}
}

As you can see, allocating and creating a jagged array is a bit more complex than rectangular arrays because you must handle all of the subarray allocations individually, whereas a rectangular array gets allocated all at once. Notice how the output provides a jagged-looking output, because each subarray has a different size:

In the example, I show two ways to iterate through the array just to show the syntax for accessing the individual items within a jagged array and how that syntax differs from accessing items within a rectangular array. The syntax is similar to that of C++ and Java. The foreach method of iterating through the array is more elegant, and as I’ll cover later on, using foreach allows you to use the same code to iterate through collections that may not be arrays.

It often makes sense to use jagged arrays rather than rectangular arrays. For example, you may be reading in information from a database, and each entry in the top-level array may represent a collection where each subcollection may have a widely varying amount of items in it. If most of the subcollections contain just a handful of items and then one of them contains 100 items, a rectangular array would waste a lot of space because it would allocate 100 entries for each subcollection no matter what. Jagged arrays are generally more space efficient, but the trade-off is that accessing items within a jagged array requires more care, because you cannot assume that each subarray has the same number of items in it.

It’s preferable to use foreach to iterate through arrays and collections. That way, you can change the type of the container later and the foreach block won’t have to change. If you use a for loop instead, you may have to change the method used to access each individual element. Additionally, foreach handles cases where the array has a nonzero lower bound.

Jagged arrays can potentially be more computationally efficient, because jagged arrays are typically arrays of single-dimension, zero-lower-bound arrays, which the CLR represents with vectors.

Source Of Information : Apress Accelerated C Sharp 2010

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner