I have an Array of unknown dimensions. E.g. it may be object[], object[,], object[,,,].
I want to fill it sequentially (e.g. for [2,2] this order: 0,0; 0,1, 1,0; 1,1):
Array arr = ... // input array
for (int i = 0; i < arr.Length; i++)
{
arr.SetValue(stream.ReadNextObject(), ???); // convert i -> int[] indexes
}
I know that the conversation of i can be done with % operator but it's hard to imagine an exact algorithm for multiple dimensions. There is only an answer for two-dimensions: Converting index of one dimensional array into two dimensional array i. e. row and column
I could use Stack<int> to store indexes while walking an array but it seems that % will be more efficient (I really need to care about performance here). But I'm not sure about Stack<T> vs %.
iis already going over every possible index in your array, why don't you usearr.SetValue(stream.ReadNextObject(), i);?switch/case. Why array is of unknown dimensions? How and from where you get it?IEnumerableis the most basic) and if it is - then run same method recursively for this element. This will work for jagged arrayList<List<List<...>>>. Not sure what to do with multidimensional arrays though.