By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,959 Members | 2,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,959 IT Pros & Developers. It's quick & easy.

How to iterate the second dimension of a 2-dimensional array?

P: n/a
Hello,

I want to iterate the second dimension of a 2-dim-array.

Let´s say I have an array:

double[,] myArray and a given index: int i.

Assume my index is given in want to iterate my array with something like
this:
int i = 2;
double sum= 0.0;

for(int j = 0; j < myArray[i].Length; j++)
sum += myArray[i,j];

But this does not work, because I am using the 2dim-array as 1dim-array.
My problem is... how do i get the size of the second dimension?
Regards,

Martin
Sep 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Martin Ppping wrote:
Hello,

I want to iterate the second dimension of a 2-dim-array.

Lets say I have an array:

double[,] myArray and a given index: int i.

Assume my index is given in want to iterate my array with something like
this:
int i = 2;
double sum= 0.0;

for(int j = 0; j < myArray[i].Length; j++)
sum += myArray[i,j];

But this does not work, because I am using the 2dim-array as 1dim-array.
My problem is... how do i get the size of the second dimension?
Regards,

Martin
double[,] a=new double[10,10];

for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i,j]=i*j;
}
}
//or try
foreach(double d in a)
{
Console.WriteLine(d.ToString());
}

Sep 26 '06 #2

P: n/a
DeveloperX schrieb:
double[,] a=new double[10,10];

for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i,j]=i*j;
}
}
Well maybe you missunderstand my problem.
I do not know the second dimension.
In your example.... I do not know that j = 10.
//or try
foreach(double d in a)
{
Console.WriteLine(d.ToString());
}
If it would work... it would traverse the whole dimensional array.

I only want to work on a[2,j] for example.
Regards,

Martin

Sep 26 '06 #3

P: n/a
Martin P?pping <ma******@despammed.comwrote:
I want to iterate the second dimension of a 2-dim-array.

Let?s say I have an array:

double[,] myArray and a given index: int i.

Assume my index is given in want to iterate my array with something like
this:
int i = 2;
double sum= 0.0;

for(int j = 0; j < myArray[i].Length; j++)
sum += myArray[i,j];

But this does not work, because I am using the 2dim-array as 1dim-array.
My problem is... how do i get the size of the second dimension?
Use Array.GetLength(int dimension)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #4

P: n/a
Martin,

If you don't know anything about the array then you need to use the Array
class method and properties. Array is a base class for all arrays and as
such all arrays have them.
Check MSDN docs. Here are some usefull methods and proeprties that you may
want to use:
- Length (property) - total number of elements in all dimensions
- Rank (property) - number of dimensions
- GetLength (method) - return number of element is a specified dimension.
--
HTH
Stoitcho Goutsev (100)

"Martin Ppping" <ma******@despammed.comwrote in message
news:ef**********@newsreader2.netcologne.de...
DeveloperX schrieb:
>double[,] a=new double[10,10];

for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i,j]=i*j;
}
}

Well maybe you missunderstand my problem.
I do not know the second dimension.
In your example.... I do not know that j = 10.
>//or try
foreach(double d in a)
{
Console.WriteLine(d.ToString());
}

If it would work... it would traverse the whole dimensional array.

I only want to work on a[2,j] for example.
Regards,

Martin

Sep 26 '06 #5

P: n/a
In addition there is

GetUpperBound(dimension), returning the last index (length - 1)
On Tue, 26 Sep 2006 16:04:32 +0200, Stoitcho Goutsev (100) <10*@100.com>
wrote:
Martin,

If you don't know anything about the array then you need to use the Array
class method and properties. Array is a base class for all arrays and as
such all arrays have them.
Check MSDN docs. Here are some usefull methods and proeprties that you
may
want to use:
- Length (property) - total number of elements in all dimensions
- Rank (property) - number of dimensions
- GetLength (method) - return number of element is a specified dimension.



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.