1

I am just starting out with C#. How can I effectively display this two-dimensional array in a console window?

string[,] dniTygodnia;
            dniTygodnia = new string[6, 6];
            dniTygodnia[0, 0] = "0.0";
            dniTygodnia[1, 0] = "1.0"; 
            dniTygodnia[2, 0] = "2.0"; 
            dniTygodnia[2, 1] = "2.1"; 
            dniTygodnia[2, 2] = "2.2";
            dniTygodnia[3, 0] = "3.0";
            dniTygodnia[3, 1] = "3.1";
            dniTygodnia[3, 2] = "3.2";
            dniTygodnia[4, 0] = "4.0";
            dniTygodnia[4, 1] = "4.1"; 
            dniTygodnia[4, 2] = "4.2";
            dniTygodnia[5, 0] = "5.0"; 
            dniTygodnia[5, 1] = "5.1";
            dniTygodnia[5, 2] = "5.2"; 
            dniTygodnia[6, 0] = "6.0"; 
            dniTygodnia[6, 1] = "6.1"; 
            dniTygodnia[6, 2] = "6.2"; 
            dniTygodnia[0, 1] = "0.1"; 
            dniTygodnia[1, 1] = "1.1"; 
            dniTygodnia[0, 2] = "0.2"; 
            dniTygodnia[1, 2] = "1.2"; 
            Console.WriteLine(dniTygodnia[6,6]);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
NIKEN
  • 19
  • 5
  • 2
    Does this answer your question? [How to print 2D array to console in C#](https://stackoverflow.com/questions/24094093/how-to-print-2d-array-to-console-in-c-sharp) – Selim Yildiz Dec 17 '19 at 20:52
  • 1
    What have you tried to do to display an array? Please show us so we can help you out. – Trevor Dec 17 '19 at 20:59

1 Answers1

3

Make sure your array has indexes from 0 to 5. So something like dniTygodnia [6, 0] = "6.0"; lead to error!

What you want to do is

for (int i = 0; i < dniTygodnia.GetLength(0); i++)
{
   for (int j = 0; j < dniTygodnia.GetLength(1); j++)
   {
      Console.WriteLine(dniTygodnia[i,j]);
   }
}

Working example:

string[,] dniTygodnia;
dniTygodnia = new string[6, 6];
dniTygodnia[0, 0] = "0.0";
dniTygodnia[1, 0] = "1.0";
dniTygodnia[2, 0] = "2.0";
dniTygodnia[2, 1] = "2.1";
dniTygodnia[2, 2] = "2.2";
dniTygodnia[3, 0] = "3.0";
dniTygodnia[3, 1] = "3.1";
dniTygodnia[3, 2] = "3.2";
dniTygodnia[4, 0] = "4.0";
dniTygodnia[4, 1] = "4.1";
dniTygodnia[4, 2] = "4.2";
dniTygodnia[5, 0] = "5.0";
dniTygodnia[5, 1] = "5.1";
dniTygodnia[5, 2] = "5.2";
dniTygodnia[0, 1] = "0.1";
dniTygodnia[1, 1] = "1.1";
dniTygodnia[0, 2] = "0.2";
dniTygodnia[1, 2] = "1.2";
for (int i = 0; i < dniTygodnia.GetLength(0); i++)
   {
      for (int j = 0; j < dniTygodnia.GetLength(1); j++)
      {
         Console.WriteLine(dniTygodnia[i, j]);
      }
   }
// Console.ReadKey(); // - If you want to see what was printed on the console.

NOTE: Array.GetLength(Int32) Method is used to get the length of a multidimensional array.

deralbert
  • 856
  • 2
  • 15
  • 33
  • 2
    The only thing I would add is changing the `i < 6` to `i < dniTygodnia.Length` that way it doesn't matter how big the array is. – Jacob Dec 17 '19 at 20:54
  • @Jacob, yes, that is the right suggestion. – deralbert Dec 17 '19 at 20:57
  • Ok, I understand it now. Thank you – NIKEN Dec 17 '19 at 21:11
  • @Jacob, because he uses a multi-dimensional array, he needs a special method to determine the array size. In his case `dniTygodnia.Length` will get the total number of elements in all the dimensions of the array (in this specific case, 6x6 Array, it will get the number 36). – deralbert Dec 17 '19 at 21:18
  • 1
    Use `dniTygodnia.GetLength(0)`, `dniTygodnia.GetLength(1)` to get the length of each dimension. – haldo Dec 17 '19 at 21:38