By Abani Pattanayak
Overview
I thank the editor for publishing my first article ie 'Why C#' in csharphelp.com and also thankful to its reader for 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 the conventional progarmming (non-OO) world of 'Visual Basic' or like, the conecpt of 'Jagged Array' may be grey area. So I thought to pen down my understaning on this topic.
Rectangular Arrays
This is the arrays datatype, most of us are familiar with. Rectangular arrays may be may be single-dimensional or multi-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 the declaration of an array variable merely sets aside space for the reference to the array. Array instances are actually created via array initializers 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 now in all languanges for .Net Framework arrays are 0-based arrays.(In Visual 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 of arrays. 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 array creation 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 rectangular shape. For example, the length of r3's three dimensions are 10, 20, and 30 respectively, and it is easy to see that this array contains 10*20*30 elements.
The variable j2 is a "jagged" array. Specifically, j2 denotes an array of an array of int, or a single-dimensional array of type int[]. Each of these int[] variables can be initialized individually, and this allows the array to take on a jagged shape. The example gives each of the int[] arrays a different length. Specifically, the length of j2[0] is 3, the length of j2[1] is 6, and the length of j2[2] is 9.