Newbie question - multidimensional arrays

P

Peter Webb

I have a whole lot of methods which look like:

public bool [,] somematrixoperation(bool [,] thismatrix) {}

I want to run this method against multiple bool[,] objects held in an array.
They all have the same size.

What I really want is a one dimensional array of two dimensional arrays.
Something like:

bool [,] [] matrixcollection = new bool [10,10] [5]

Then I can call:

somematrixoperation(matrixcollection); // matrixcollection is a 2D
array

Unfortunately, the notation bool [,] [] defines a jagged array, and not an
1D array of 2D objects as I want.

Is there some other notation that I can use, or some easier approach than
changing my methods to accept 3D arrays instead of 2D arrays?

Thanks


Peter Webb
 
P

Pavel Minaev

I have a whole lot of methods which look like:

public bool [,] somematrixoperation(bool [,] thismatrix) {}

I want to run this method against multiple bool[,] objects held in an array.
They all have the same size.

What I really want is a one dimensional array of two dimensional arrays.
Something like:

bool [,] [] matrixcollection = new bool [10,10] [5]

Then I can call:

somematrixoperation(matrixcollection); // matrixcollection is a 2D
array

Unfortunately, the notation bool [,] [] defines a jagged array, and not an
1D array of 2D objects as I want.

Is there some other notation that I can use, or some easier approach than
changing my methods to accept 3D arrays instead of 2D arrays?


It seems that the sole reason why you do not like the jagged array
solution is because you want to enforce that constraint of "all
elements have the same dimensions" - otherwise I don't see anything
wrong with it. If so, why not make your own Collection<T>-derived
class that will enforce that?

Other than that, I don't really have any suggestions for you. You
can't slice a dimension out of a multidimensional array in C#, nor do
you have true arrays of arrays - only arrays of references to arrays.
 
M

miher

Hi,
I might not get Your problem right, but is this what You want to do? :

bool [][,] matrixcollection = new bool[numberOfMatrices][,];
for (int i = 0; i < matrixcollection.Length; i++)
matrixcollection = SomeMatrix(i);
......
// do some operation on 1st element
somematrixoperation(matrixcollection[0]);

-Zsolt
 

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