Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Search Your Question

Monday, May 5, 2008

What are jagged arrays?

A jagged array is an array whose elements are arrays.
The elements of jagged array can be of different dimensions and sizes. A jagged array is sometimes called as “array-of-arrays”. It is called jagged because each of its rows is of different size so the final or graphical representation is not a square.

For e.g. :

int [] [] myJaggedArray = new int [3][];

Declaration of inner arrays:
myJaggedArray[0] = new int[5] ; // First inner array will be of length 5.
myJaggedArray[1] = new int[4] ; // Second inner array will be of length 4.
myJaggedArray[2] = new int[3] ; // Third inner array will be of length 3.

Now to access third element of second row we write:

int value = myJaggedArray[1][2];

While declaring the array the second dimension is not supplied because this you will declare later on in the code.

Since Jagged array are created out of single dimensional arrays don’t confuse it with multi-dimensional arrays because unlike them jagged arrays are not rectangular arrays.

No comments:

Archives