Jagged Arrays In C# – What’s That?


Overview

I thank the editor for publishing my firstarticle ie 'Why C#' in csharphelp.com and also thankful to its readerfor their constructive suggestions and comments.

This article discusses about the most talked datatype in the programming world – ARRAYS. C# offers two type of Arrays.

1. Rectangular Arrays
2. Jagged Arrays .

For those thaousand of programmers from theconventional progarmming (non-OO) world of 'Visual Basic' or like, theconecpt of 'Jagged Array' may be grey area. So I thought to pen down myunderstaning on this topic.

Rectangular Arrays

This is the arrays datatype, most of us arefamiliar with. Rectangular arrays may be may be single-dimensional ormulti-dimensional.

Declaring single dimenisonal arrays.

short[] shtEmpNo;
int[] intSalary;

Declaring multi-dimenisonal arrays.
// two-dimensional arrays of short
short[,] shtEmpNo;
// three-dimensional arrays of int
int[,,] intSalary;

Rule: Element-type (int, short, long) Rank-specifiers ([], [,,]) Name (Arrays Name)

Array types are reference types, and so thedeclaration of an array variable merely sets aside space for thereference to the array. Array instances are actually created via arrayinitializers and array creation expressions

Intialising Arrays

// 5 member single-dimensional arrays intialised
short[] shtEmpNo = new short[5];
// 3 member single-dimensional arrays
int[] intSlNo = new int[] {1, 2, 3};
// 3*2 member two-dimensional arrays
int[,] intCount = new int[,] {{1, 2, 3}, {4, 5, 6}};
// 1*3 member three-dimesional arrays.
int[,,] intDec = new int[10, 20, 30];

In C# supports 0-based arrays only. Infact nowin all languanges for .Net Framework arrays are 0-based arrays.(InVisual Basic 6.0 Arrays could be Non-Zero based). So

shtEmpNo[0] = 0 ….. shtEmpNo[4] = 0
intSlNo[0] = 1 , intSlNo[1] = 2 and intSlNo[2] = 3
intCount[0,0] = 1, intCount[1,0] = 2, intCount[0,1] = 4 and intCount[2,2] = 6
intDec[0,0,0] = 10 and intDec[0,0,2] = 30

Here is a simple example

using System;
class MyClass
{
static void Main() {
int[] intDec = new int[5];
for (int i = 0; i < intDec.Length; i++)
intDec[i] = i * 10;
for (int i = 0; i < intDec.Length; i++)
Console.WriteLine("intDec[{0}] = {1}", i, intDec[i]);
}
}
The program output is:
intDec[0] = 0
intDec[1] = 10
intDec[2] = 20
intDec[3] = 30
intDec[4] = 40

Jagged Arrays

Jagged arrrays are nothing but arrays ofarrays. This is very clear from the 'Declaration' sysnatx. See the []appears more than once in the following declaration.

Declaring Jagged Arrays

// "jagged" array: array of (array of int)
int[][] j2;
// array of (array of (array of int))
int[][][] j3;

Rectangualr Arrays ~ Jagged Arrays

//single-dimensional rectangukar arrays
int[] r1 = new int[] {1, 2, 3};
//two-dimensional rectangualar arrays
int[,] r2 = new int[,] {{1, 2, 3}, {4, 5, 6}};
//three-dimesional rectangular arrays
int[,,] r3 = new int[10, 20, 30];
//"jagged" array: araay of(array of int)
int[][] j2 = new int[3][];
j2[0] = new int[] {1, 2, 3};
j2[1] = new int[] {1, 2, 3, 4, 5, 6};
j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

The above illustartions shows variety of arraycreation expressions (both recatngular and jagged). The variables r1,r2 and r3 are rectangular arrays and the variable j2 is a jagged array.

Rectangular arrays always have a rectangularshape. For example, the length of r3's three dimensions are 10, 20, and30 respectively, and it is easy to see that this array contains10*20*30 elements.

The variable j2 is a "jagged" array.Specifically, j2 denotes an array of an array of int, or asingle-dimensional array of type int[]. Each of these int[] variablescan be initialized individually, and this allows the array to take on ajagged shape. The example gives each of the int[] arrays a differentlength. Specifically, the length of j2[0] is 3, the length of j2[1] is6, and the length of j2[2] is 9.

Related Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!