Multidimensional arrays
Telerik Academy Alpha

 

Table of contents

Multidimensional arrays

 

Multidimensional Arrays

  • Matrices or tables

  • C# has multidimensional arrays as well as jagged array

    • ​Creating

       

    • Initializing

int[,] matrix = new int[2, 4];
int[,] matrix = 
{
   // cols
   // 0  1  2  3   
    { 1, 2, 3, 4 }, // row 0
    { 5, 6, 7, 8 }  // row 1
};
1 2 3 4
5 6 7 8

Multidimensional Arrays

  • Accessing the elements of the matrix

Console.WriteLine(matrix[0, 0]); // 1
Console.WriteLine(matrix[1, 2]); // 7
Console.WriteLine(matrix[1, 4]); // Out of boundaries
  • Could have more than 2 dimensions

int[,,] cube = 
{
    { 
        { 1, 2, 3 }, 
        { 4, 5, 6 }    
    },
    { 
        { 7, 8, 9 }, 
        { 10, 11, 12 } 
    }
};

Jagged Arrays

 Jagged Arrays

  • Array of arrays
  • Every sub-array can have different size
1 2 3 4 5
0 -1 -2
8 7 6 5
int[][] jagged = new int[3][];
jagged[0] = new int[5] {1, 2, 3, 4, 5};
jagged[1] = new int[3] {0, -1, -2};
jagged[2] = new int[4] {8, 7, 6, 5};

Reading and Printing

 Reading Multidimensional Array

// Read from the Console. It could be read from any source (ex: file)
int rows = int.Parse(Console.ReadLine()); 
int columns = int.Parse(Console.ReadLine());

int[,] matrix = new int[rows, columns];

for (int row = 0; row < rows; row++)
{
    for (int column = 0; column < columns; column++)
    {
        // Interpolation strings
        Console.Write($"matrix[{row},{column}] = ");
        matrix[row, column] = int.Parse(Console.ReadLine());
    }
}

 Reading Multidimensional Array - live demo

for (int row = 0; row < matrix.GetLength(0); row++)
{
    for (int col = 0; col < matrix.GetLength(1); col++)
    {
        Console.Write("{0, 4}", matrix[row, col]);
    }

    Console.WriteLine();
}

 Printing Multidimensional Array

 Printing Multidimensional Array - live demo

int size = int.Parse(Console.ReadLine());

string[][] jagged = new string[size][];

for (int i = 0; i < size; i++)
{
    string line = Console.ReadLine();
    // Split the line by "," or any other symbol 
    string[] arrStrings = line.Split(',');

    // Assign the split array to the jagged at position "i"
    jagged[i] = arrStrings;
}

 Reading Jagged Array

 Reading Jagged Array - live demo

// jagged array from the previous example
for (int i = 0; i < jagged.Length; i++)
{
    for (int j = 0; j < jagged[i].Length; j++)
    {
        Console.Write(jagged[i][j]);
    }
    Console.WriteLine();
}

 Printing Jagged Array

// jagged array from the previous example
for (int i = 0; i < jagged.Length; i++)
{
    Console.WriteLine(string.Join(",", jagged[i]));
}

 Printing Jagged Array - live demo

Hints

 Hints

  • Arrays are passed by reference
    • To be sure that given method will not change the passed array, pass a copy of it
       
  • Clone()  returns shallow copy of the array
    • You should implement your own deep clone when working with custom reference types

 Copy arrays

int[] arr = { 1, 2, 3 };
int[] arrCopy = arr;

arr[1] = 5;

// The result is 5 because the two arrays 
// points to the same address in memory
Console.WriteLine(arrCopy[1]);

// True -> The same address in memory
Console.WriteLine(arr == arrCopy);

 Copy arrays - live demo

 Copy arrays - Shallow copy

int[][] arr = new int[2][];

arr[0] = new int[3] { 1, 2, 3 };
arr[1] = new int[1] { 2 };

// Clone and cast it to int[]
int[][] arrCopy = (int[][])arr.Clone();

arr[1][0] = 20;

// Result is 20 because the elements of the array are arrays 
// and they are reference types
Console.WriteLine(arrCopy[1][0]);

// False -> Shallow copy (different arrays but the same addresses of the values)
Console.WriteLine(arr == arrCopy);

 Copy arrays - Shallow copy

 Copy arrays - Shallow copy

Questions?

[C#] Multidimensional Arrays

By telerikacademy

[C#] Multidimensional Arrays

  • 1,147