mercredi 15 avril 2015

How to iterate over each item of a multi-dimensional array?

Let's say I get an anonymous instance of a matrix or deep multi-dimensional array.



byte[,] a = new byte[,] { { 64, 65 } , { 66, 67 } }
byte[, , , ,] b = new byte[,,,,] { { { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 9, 10 }, { 11, 12 } }, { { 13, 14 }, { 15, 16 } } } }, { { { { 17, 18 }, { 19, 20 } }, { { 21, 22 }, { 23, 24 } } }, { { { 25, 26 }, { 27, 28 } }, { { 29, 30 }, { 31, 32 } } } } };
// not jagged arrays


So when the user goes nuts - btw this is for a library, I need to know if I can manage any dimension of arrays (jagged array are simple though) - how do I navigate myself safely through the maze of those dimensions (via reflection)?


Can I iterate over the whole array, over each element, in the right order?



64, 65, 66, 67
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 32


I wished to avoid nested for-loops since I can't cover any number of dimension with 2 or 3 loops. Would foreach give me the right order?






Aucun commentaire:

Enregistrer un commentaire