Topic : Two-dimensional Arrays in C#: Declaring Arrays | Initializing Arrays | Accessing Array Elements | Three-Dimensional Arrays



Multi-Dimensional Arrays:

Arrays can also be used to represent Multidimensional data, like a chess board, or like a tic-toe board. Multidimensional data just means that we have more than one index for it. The simplest form of the Multidimensional Array is the "Two Dimensional Array".



Two Dimensional Array:

The Simplest form of the Multi-dimensional Array is the two dimensional arrays. two-dimensional array is the list of one-dimensional arrays. A two-dimensional array can be thought of as a table, which has x number of rows and y number of columns. Following is a two-dimensional array, which contains three rows and four columns.



Multidimensional Arrays: Infobrother:

Thus every element in the array "a" is identified by an element name of the form a[ i, j ] where "a" is the name of the array, and "i" and "j" are the subscripts that uniquely identify each element in array "a".



Declaring Two-dimensional Arrays:

To Declare an two-dimensional Array of size x,y, we would write the following syntax:


Data-Type [ , ]   variable-name   = new   Data-Type [x , y];


  • In Above Syntax:

    » Data-Type could be any data type like int or double or char.
    » [ , ] this indicates that this is two-dimensional Array (2D).
    » Variable-name could be any name for the variable to represent array.
    » [ x , y ] represents rows and columns. x represent rows and y represent columns.



Initializing Two-Dimensional Arrays:

We can Initialized Multidimensional arrays by specifying bracketed values for each row. C# don't have any special syntax to initialize the multidimensional arrays. On the contrary, an N-dimensional Array is no Different than an Array with only one Dimension whose elements are (N-1) Dimensional Arrays.

 // Data-type [,] variable-name = new  Data-type[x,y];
 int[,] number = new int[3, 3]  //3-rows and 3-columns:
 {
   {0, 1, 2 },  //initializes for row indexed by 0 
   {3, 4, 5 },  //initializes for row indexed by 1 
   {6, 7, 8 }   //initializes for row indexed by 2
 };


Accessing Two-Dimensional Array Elements:

Once an Array is Filled with useful values, we can access and use an individual array element in the same manner we would access and use any single variable of the same type. we Can access any Element of Array by indexing the array name. This is done by using Subscripts, i.e. Row index and column index of the Array:


 // Data-type [,] variable-name = new  Data-type[x,y];
 int[,] number = new int[3, 3]  //3-rows and 3-columns:
 {
   {0, 1, 2 },  //initializes for row indexed by 0 
   {3, 4, 5 },  //initializes for row indexed by 1 
   {6, 7, 8 }   //initializes for row indexed by 2
 };

//access 2nd element from 3rd row. (i.e. 7):
int access = number[2, 1];


Example:

Let's have an example here to know how we can create multi-dimensional array, how to initialize the array, how to access it. in this example we will create the Array of type string and will store some name and display all name using for-loop.



/*Example: Multi-Dimensional Arrays: InfoBrother*/

using System;

namespace multi_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            // Data-type [,] variable-name = new  Data-type[x,y];
            string[,] name = new string[3, 3]  //3-rows and 3-columns:
            {
                {"info", "Brother", "Omar" },  //initializes for row indexed by 0 
                {"Taimur", "Usman", "Khizar" },  //initializes for row indexed by 1 
                {"Ayan", "Sarim", "Dani" }   //initializes for row indexed by 2
            };

            //output each element of Arrays:
            for(int x=0; x<3; x++)   //for rows:
            {
                for(int y=0; y<3; y++)  //for columns: 
                {
                    Console.WriteLine("name[{0},{1}] = {2}", x, y, name[x, y]);
                }
            }
            
            Console.ReadKey();

        }
    }
}



Multi-dimensional Arrays : infobrother

We can also visualize this entire example as being like a common table structure. This is similar to a Multiplication table, where we get the value (in the grid) by matching up the row number with the column number.



Three-Dimensional Array:

We can create N-Dimensional Arrays: let's look, how we can create three-dimensional arrays:


/*Example: Three-Dimensional Arrays: InfoBrother*/

using System;

namespace _3DArray
{
    class Program
    {
        static void Main(string[] args)
        {
            //Data-type[,,] variable-name = new Data-type[x,y,z];
            int[,,] array_3D = new int[2, 3,3] 
            {
                { { 1, 2, 3 }, { 4, 5, 6 },{7, 8, 9 } },
                { { 10, 11, 12 }, { 13, 14, 15 }, {16, 17, 18} }
            };

            //output each element of Arrays:
            for (int x=0; x<2; x++)   //for x:
            {
                for(int y=0; y<3; y++)  //for y: 
                {
                    for(int z=0;  z<3; z++)   //for Z:
                    {
                        Console.WriteLine("name[{0},{1},{2}] = {3}", x, y, z, array_3D[x, y,z]);
                    }
                }
            }
            
            Console.ReadKey();

        }
    }
}


This 3D Array structure looks more complicated, but it only has one more level of sub-containment, and is best visualized as successive containers that contain other containers. Notice, again, how only the last level of containment holds an actual value while the other levels are only sub-containers:



3d array : infobrother















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement