I have an array something like this:

   int[,] multiDimensionalArray2 = { { 1, 2 }, { 4, 5 } };

if I want to retreive 1 and 2 and feed them into this:

int a;
int b;

How do I do it?

Is it something like this:

multiDimensionalArray2[0,0]

What if I wanted to put more numbers in the same form e.g. { { 2, 1 }, { 4, 1 } };, in the same form as above..would it be something like this:

   int[,] multiDimensionalArray2 = { { 1, 2 }, { 4, 5 } },{ { 2, 1 }, { 4, 1 } };

To retrieve the second set would I do this, multiDimensionalArray2[1,1]

有帮助吗?

解决方案

You´re close, to retrieve the first numbers try this

        var a = multiDimensionalArray2[0, 0]; // x == 1
        var b = multiDimensionalArray2[0, 1]; // x == 2

Did you give it a try and it didn´t work? You´ll notice that SO users will encourage you to try-and-error first. Come back when you hit a wall :)

其他提示

Double dimention arrays are stored like this (picture it in your mind) as per your example

Row0  Row1 
1       2
4       5
2       1
4       1

Now follow what Dominik suggested.

If you have a multi dimensional array like the one you gave,

int[,] multiDimensionalArray2 = { { 1, 2 }, { 4, 5 }, { 2, 1 }, { 4, 1 } };

We want to get the index of the number 5. To do this we need two indexes, [a,b]

Index a is the index of the "group of numbers" that you want to get. First look at which group it is in. The first group (index 0) contains 1 and 2, the second group (index 1) contains 4 and 5. Therefore a = 1.

Index b is the index of the "position within the group" that you want to get. In the group {4, 5}, the number 5 is the first item (index 0). Therfore b = 0.

This means that the number 5 can be found at multiDimensionalArray2[1,0]

Chao, I search documents in MSDN site for you, you should give it a try if you want.

Here is how I see on MSDN

List< > > Dictionary<>

I guess it works for your problem too, it can dynamically grow longer or shorter. they say using generic classes might be better, I am not sure about whether it ought to be faster or not though.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top