How to access only the first position of each row in an array of arrays (matrix / multidimensional array) using foreach?

Asked

Viewed 712 times

0

I have a array multidimensional / array of arrays / matrix (as you prefer to call):

    public string[,] operacoes = 
    { 
       { "SOMA", "", "" }, 
       { "SUBTRAÇÃO", "", "" }, 
       { "MULTIPLICAÇÃO", "", "" }, 
       { "DIVISÃO", "", "" } 
    };

I am using a foreach loop to return only the first position of each row. The other positions are not relevant to this case.

foreach (string op in operacoes)
{
   ...
}

What could I do to return only those positions with names of operations and ignore the rest?

  • Use a normal for.

4 answers

4

The foreach go through all the elements of the collection. To do what you need, just use a for normal.

Note that the property Length will return the amount total of elements in the collection, ie, will consider all dimensions.

In your case, you need to go through the zero dimension and always show the first element of it.

void Main()
{
    string[,] operacoes = 
    { 
       { "SOMA", "", "" }, 
       { "SUBTRAÇÃO", "", "" }, 
       { "MULTIPLICAÇÃO", "", "" }, 
       { "DIVISÃO", "", "" } 
    };

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

See working on Repl.it

4

You have to manually pick up the first column and so can not use the foreach, for that exists the forgross and the method GetLength() which gives the size of a dimension, how you want to walk in each line, and is the dimension 0.

using static System.Console;

public class Program {
    public static void Main() {
        string[,] operacoes = { 
           { "SOMA", "x", "" },
           { "SUBTRAÇÃO", "x", "" },
           { "MULTIPLICAÇÃO", "x", "" },
           { "DIVISÃO", "x", "" }
        };
        for (var i = 0; i < operacoes.GetLength(0); i++) WriteLine(operacoes[i, 0]);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

A very important detail is that we are talking about matrix here, array of arrays is something else entirely, it can have different sizes of rows and columns, while the matrix needs to be perfectly rectangular. The array of array is called Jagged array. See examples.

0

To access the first array uses multiArray[0], the second uses multiArray[1] and thus using indexing as if it were a array of only one dimension.

To access the first index within each sub array uses multiArray[index][0].

Here is a Javascript example that you can run and see the result:

 var multiArray = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]];

 console.log('Números no primeiro index do array multi dimensional')
 multiArray[0].forEach((value) => console.log(value));

 console.log('Números no primeiro index dos arrays dentro do array dimensional')
 multiArray.forEach((value) => console.log(value[0]));

0

It is possible to use foreach become the array in one-dimensional, taking only the first position of the objects:

string[,] operacoes = {
   { "SOMA", "x", "" },
   { "SUBTRAÇÃO", "x", "" },
   { "MULTIPLICAÇÃO", "x", "" },
   { "DIVISÃO", "x", "" }
};


var results = Enumerable
    .Range(0, operacoes.GetUpperBound(0) + 1)
    .Select(indice => operacoes[indice, 0])
    .ToArray();

foreach (var i in results)
{
    Console.WriteLine(i);
}

In this code I used the Enumerable.Range to generate the indexes for the size of the array, from zero to size (operacoes.GetUpperBound(0) + 1).

Then the Select selects only the first element of each index [0,0], [1,0], etc... using the indice, which were the indices generated by .Range.

Finally, the ToArray() convert to array, then do the foreach

Here the working code: dotnetfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.