Sunday, March 13, 2022

Array

 

multidimensional arrays up to 32 dimensions. The multidimensional array can be declared by adding commas in the square brackets. For example, [,] declares two-dimensional array, [, ,] declares three-dimensional array, [, , ,] declares four-dimensional array, and so on. So, in a multidimensional array, no of commas = No of Dimensions - 1.

The following declares multidimensional arrays.

int[,] arr2d; // two-dimensional array

int[, ,] arr3d; // three-dimensional array

int[, , ,] arr4d ; // four-dimensional array

int[, , , ,] arr5d; // five-dimensional array

 

 

 

int[,] arr2d = new int[3,2]{ 
                                {1, 2}, 
                                {3, 4}, 
                                {5, 6} 
                            };
 
// or 
int[,] arr2d = { 
                    {1, 2}, 
                    {3, 4}, 
                    {5, 6} 
                };

 

 nt[,] arr2d = new int[3, 2]{

        {1, 2},
        {3, 4},
        {5, 6}
       };

   Console.WriteLine(arr2d[0, 0]);
   Console.WriteLine(arr2d[0, 1]);
   Console.WriteLine(arr2d[1, 0]);
   Console.WriteLine(arr2d[1, 1]);
   Console.WriteLine(arr2d[2, 0]);
   Console.WriteLine(arr2d[2, 1]);
   int[,,] arr3d1 = new int[1, 2, 2]{
   {{1, 2}, {3, 4}} // 1 row of two-dimensional array
  };

   int[,,] arr3d2 = new int[2, 2, 2]{ // 2 rows of two-dimensional array
   {{1, 2}, {3, 4}},
   {{5, 6}, {7, 8}}
  };

   int[,,] arr3d3 = new int[2, 2, 3]{ // 2 rows of two-dimensional array
   {{1, 2, 3}, {4, 5, 6}},
   {{7, 8, 9}, {10, 11, 12}}
  };

   Console.WriteLine("arr3d2 Values");
   Console.WriteLine(arr3d2[0, 0, 0]);
   Console.WriteLine(arr3d2[0, 0, 1]);
   Console.WriteLine(arr3d2[0, 1, 0]);
   Console.WriteLine(arr3d2[0, 1, 1]);
   Console.WriteLine(arr3d2[1, 0, 0]);
   Console.WriteLine(arr3d2[1, 0, 1]);
   Console.WriteLine(arr3d2[1, 1, 0]);
   Console.WriteLine(arr3d2[1, 1, 1]);


 

 

 

A jagged array is an array of array. Jagged arrays store arrays instead of literal values.

A jagged array is initialized with two square brackets [][]. The first bracket specifies the size of an array, and the second bracket specifies the dimensions of the array which is going to be stored.

 

 

 

int[][] jArray1 = new int[2][]; // can include two single-dimensional arrays 
int[][,] jArray2 = new int[3][,]; // can include three two-dimensional arrays 

 

 

int[][] jArray = new int[2][]; 
 
jArray[0] = new int[3]{1, 2, 3};
 
jArray[1] = new int[4]{4, 5, 6, 7 };

 

 // int[][] jArray = new int[2][]{

// new int[3]{1, 2, 3},


// new int[4]{4, 5, 6, 7}

//};


// Console.WriteLine(jArray[0][0]);

// Console.WriteLine(jArray[0][1]);

// Console.WriteLine(jArray[0][2]);

// Console.WriteLine(jArray[1][0]);

// Console.WriteLine(jArray[1][1]);

// Console.WriteLine(jArray[1][2]);

// Console.WriteLine(jArray[1][3]);


//int[][] jArray = new int[2][]{

// new int[3]{1, 2, 3},


// new int[4]{4, 5, 6, 7}

//};


//for (int i = 0; i < jArray.Length; i++)

//{

// for (int j = 0; j < (jArray[i]).Length; j++)

// Console.WriteLine(jArray[i][j]);

//}

No comments:

Post a Comment