Home / Articles / Programming Languages / C# / C# Arrays and Strings I / Rectangular & Jagged Arrays

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

C# Programming Series

C# Arrays and Strings I

Yashavant Kanetkar
Yashavant Kanetkar

Index

  1. Arrays Defined
  2. Rectangular & Jagged Arrays

Rectangular Arrays

In rectangular arrays every row of the array is of the same length. This means if we have a two-dimensional array, all the rows will have same number of elements. This property is similar to arrays in C or C++. The following code snippet will give you an idea about rectangular arrays:

int [ , ] arr1 = new int [ , ] { { 3, 5, 7, 9 }, { 11, 13, 15, 17 } } ;

Or

int [ , ] arr1 = { { 2, 4, 6, 8 }, {10, 12, 14, 16 } } ;

To print the elements of the rectangular array we need to write the following code:

foreach ( int i in arr1 )
    Console.Write ( i + " " ) ;

The foreach loop allows us to iterate through each item in an array. The foreach loop always travels through all elements of one row before moving to the next row. Using foreach we can never travel through a 2-D array column by column.

Jagged Arrays

A jagged array is an array of several 1D arrays each of different length. We cannot create the jagged array in the same way as we created the rectangular array. Suppose we are required to create a jagged array containing 2 rows where first row contains four integers and the second row contains 2 integers. We may try to define this array as follows:

int [ , ] arr1 = new int [ , ] { { 3, 5, 7, 1 }, { 11, 13 } } ;

Or

int [ , ] arr1 = { { 3, 5, 7, 1 }, { 11, 13 } } ;

Both these definitions are wrong, because the number of elements in each row is different and the complier never keeps track of how many elements are present in each row. Hence later on it would not be feasible for it to access the correct element when we use an array access expression, like arr [ 1, 2 ]. The correct way to initialize the jagged array is as follows:

int[ ] [ ] arr1 = new int [ 2 ] [ ] ;
arr1 [ 0 ] = new int [ 3 ] { 3 , 5, 7, 1 } ;
arr1 [ 1 ] = new int [ 2 ] { 11, 13 } ;

The in-memory view of this jagged array is as follows:

The way to create a 3D jagged array is as follows:

int[ ] [ ] [ ] arr = new int [ 2 ] [ ] [ ] ;
arr [ 0 ] = new int [ 3 ] [ ] ;
arr [ 0 ] [ 0 ] = new int [ 3 ] ;
arr [ 0 ] [ 1 ] = new int [ 4 ] ;
arr [ 0 ] [ 2 ] = new int [ 5] ;

Here the 3D array consists of two 2D arrays. The first 2D array would consist of three 1D arrays, and finally all the three 1D arrays have different number of elements.

From the above discussion it is clear that in a rectangular array all indices are within one set of brackets. for a jagged array each element is within its own bracket. For a 2D rectangular array we have to write int [ , ] whereas, for a 2-D jagged array we have to write int[ ][ ].

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts