Searching...

Translate

Multidimensional Arrays

v Multidimensional Arrays

   Although the one-dimensional array is the most commonly used array in programming, multidimensional arrays are certainly not rare. A multidimensional array is an array that has two or more dimensions, and an individual element is accessed through the combination of two or more indices.

Þ   Two-Dimensional Arrays

   The simplest form of the multidimensional array is the two-dimensional array. In a two dimensional array, the location of any specific element is specified by two indices.

    If you think of a two-dimensional array as a table of information, one index indicates the row, the other indicates the column. To declare a two-dimensional integer array table of size 3, 3 you would write

SYNTAX :- int[,] table = new int[10, 20];

   To access an element in a two-dimensional array, you must specify both indices, separating the two with a comma. For example, to assign the value 10 to location 1, 2 of array table, you would use

table[1, 2] = 10;

Þ   Example
using System;

class TwoD
{
static void Main()
{
int t, i;
int[,] table = new int[3, 3];
for(t=0; t < 3; ++t)
{
for(i=0; i < 3; ++i)
{
table[t,i] = (t*4)+i+1;
Console.Write(table[t,i] + " ");
}
Console.WriteLine();
}
}
}

   In this example, table[0, 0] will have the value 1, table[0, 1] the value 2, table[0, 2] the value 3, and so on.



v Jagged Arrays

   When you created a two-dimensional array, you were creating what C# calls a rectangular array. Thinking of two-dimensional arrays as tables, a rectangular array is a two-dimensional array in which the length of each row is the same for the entire array.

   However, C# also allows you to create a special type of two-dimensional array called a jagged array. A jagged array is an array of arrays in which the length of each array can differ. Thus, a jagged array can be used to create a table in which the lengths of the rows are not the same.

   Jagged arrays are declared by using sets of square brackets to indicate each dimension. For example, to declare a two-dimensional jagged array, you will use this general form:

type[][] array-name = new type[size][];

   Here, size indicates the number of rows in the array. The rows, themselves, have not been allocated. Instead, the rows are allocated individually. This allows for the length of each row to vary.

   For example, the following code allocates memory for the first dimension of jagged when it is declared. It then allocates the second dimensions manually.

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

   After this sequence executes jagged looks like this:
JAGEDARR



   Once a jagged array has been created, an element is accessed by specifying each index within its own set of brackets. For example, to assign the value 10 to element 2, 1 of jagged, you would use this statement:

jagged[2][1] = 10;

Þ   Example
using System;

class Program
{
static void Main(string[] args)
   {
         int[][] arr=new int[3][];
         arr[0]=new int[2];
         arr[1]=new int[4];
         arr[2]=new int[3];
         Console.WriteLine("Enter element in jagged array..");
         for (int i = 0; i < 3; i++)
         {
               Console.WriteLine("Enter element in {0} row", i + 1);
               for (int j = 0; j < arr[i].Length; j++)
               {
                  arr[i][j]=Convert.ToInt32(Console.ReadLine());
               }
            }
         Console.WriteLine("The Element in jagged arr is as below");
         for (int i = 0; i < 3; i++)
         {
               for (int j = 0; j < arr[i].Length; j++)
               {
                     Console.Write("{0}  ", arr[i][j]);
               }
               Console.WriteLine();
         }
         Console.ReadLine();
        }
    }

   The output is shown here:

1 1
2 2 2 2
3 3 3











0 comments:

Post a Comment

Subscribe us