multidimensional array

P

puzzlecracker

Does dimension o start vertically or horizontally when an array calls
GetLength() method?
In this example, just trying to understand the order it will print it,
and if I extend it to more dimensions.


static void Main()
{
int[,] twoDim = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
for( int i = 0; i != twoDim.GetLength(0); ++i )
{
for( int j = 0; j != twoDim.GetLength(1); ++j )
{
Console.WriteLine( twoDim[i,j] );
}
}

Also, is it possible to re-write this code using for_each loop?

Thanks
 
F

Family Tree Mike

The way I think of the GetLength(index) argument is that you always have a 1
dimensional array, so zero goes to the outer array size. It then goes on
from there....

To change your loop, do:

foreach (int a in twoDim) Console.WriteLine(a);

It writes it out inner index changing fastest, just as your nested for loops
go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top