简体   繁体   中英

Getting and setting the value of a specific part of a large multi-dimensional array

Say, I have an array FooArray, which consists of arrays consisting of arrays of arrays- etc.

Then I have another variable D, defining in which depth of the array I want to be looking at.

And lastly I have another array nArray of integers, which determine which array in each depth should be dug further into.

(At least, that's the way I tried to approach this)

Now my question is, how would I get/set any index of the array at depth D, in FooArray? For instance, the array may look like this:

[FooArray]
 -- [Array]
     -- [Array]
         -- [Array]
         -- [Array]
     -- [Array] <--- With D = 1 and nArray = {0,1,...}, I'd want this array.
         -- [Array]
         -- [Array]
 -- [Array]
     -- [Array]
         -- [Array]
         -- [Array]
     -- [Array]
         -- [Array]
         -- [Array]

There's probably a way to do this, but how?

Thanks in advance!

Probably you're better off using a multidimensional array :

int[, ,] a = new int[2, 2, 3]{
        { {1,2,3}, {4,5,6} },
        { {7,8,9}, {10,11,12}  }
};
int x = (int)a.GetValue(0,1,2); // this returns 6

int[] nArray = new int[] { 1, 1, 0 };
x = (int)a.GetValue(nArray); // this returns 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM