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

G

Guest

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.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
 
D

DeveloperX

Martin said:
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.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());
}
 
J

Jon Skeet [C# MVP]

Martin P?pping said:
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.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)
 
?

=?ISO-8859-1?Q?Martin_P=F6pping?=

DeveloperX said:
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
 
S

Stoitcho Goutsev \(100\)

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 Pöpping said:
DeveloperX said:
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
 
M

Morten Wennevik

In addition there is

GetUpperBound(dimension), returning the last index (length - 1)
 

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