Operations with Multidimensional Arrays

Printing a Two-Dimensional Array

If we want to process all the elements of a two-dimensional array, for example, to print all the elements into a label, we need to use a nested for loop.

According to the example in the previous topic; Let's print a 20 x 3-dimensional array containing the names, surnames and numbers of the students in a 20-element class into the label. Let's make it go to the bottom line after each of the inner strings.

string[,] my class = new string[20, 3];

myclass[0, 0] = "135";
myclass[0, 1] = "Ayten";
myclass[0, 2] = "Yilmaz";
myclass[1, 0] = "145";
myclass[1, 1] = "John";
myclass[1, 2] = "Hopkins";

// We defined the array in the first line. Let's say we enter all the elements after it. Now we can print the elements thanks to the for loops.

for (int i = 0; i <= 19; i++)
{
   for (int j = 0; j <= 2; j++)
   {
     label1.Text += myclass[i, j] + " ";
   }
label1.Text += "\n";
}

Printing a 3D Array

For example, to print a 5 x 20 x 4 array, we need 3 nested for loops. A space will be placed between each element.

for (int i = 0; i <= 4; i++)
{
   for (int j = 0; j <= 19; j++)
    {
       for (int k = 0; k <= 3; k++)
       {
         label1.Text += myclass[i, j, k] + " ";
       }
   }
}

 

printing multidimensional arrays, multidimensional array for loop, multidimensional array with for loop

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 586 times.

Online Users: 1086



operations-with-multidimensional-arrays