Mike de Klerk Mike de Klerk - 13 days ago 5
C# Question

Accessing multi-dimensional array with one-dimensional index

See the piece of code below. What is the way to access a multi-dimensional array with a one-dimensional index. Foreach can do it. Yeah I know, IEnumerable with yield isn't the same as an index. Should I use a foreach and create a new array? Or can I do it without creating a new array?

int[,] myArray = new int[2, 2];
myArray[0,0] = 1;
myArray[1,1] = 2;
myArray[0,0] = 3;
myArray[1,1] = 4;
foreach (var value in myArray)
{
Console.Write(value);
}

var valueAtIndex = myArray[2]; //Won't compile, error: Wrong number of indices inside []; expected 2

Answer

If you want not just to read the values but also set them in the same order as foreach traverses the array, you can use the following general indexer class:

public class ArrayIndexer
{
    readonly int totalLength;
    readonly int lastIndexLength;
    readonly int[] lengths;
    readonly int[] lowerBounds;
    int current;
    readonly int[] currentZeroBased;

    public ArrayIndexer(int[] lengths, int[] lowerBounds)
    {
        lastIndexLength = lengths[lengths.Length - 1];
        totalLength = lengths[0];
        for (int i = 1; i < lengths.Length; i++)
        {
            totalLength *= lengths[i];
        }
        this.lengths = lengths;
        this.lowerBounds = lowerBounds;
        currentZeroBased = new int[lengths.Length];
        current = -1;
    }

    public bool MoveNext()
    {
        current++;
        if (current != 0)
        {
            int currLastIndex = current % lastIndexLength;
            currentZeroBased[currentZeroBased.Length - 1] = currLastIndex;
            if (currLastIndex == 0)
            {
                for (int i = currentZeroBased.Length - 2; i >= 0; i--)
                {
                    currentZeroBased[i]++;
                    if (currentZeroBased[i] != lengths[i])
                        break;
                    currentZeroBased[i] = 0;
                }
            }
        }
        return current < totalLength;
    }

    public int[] Current
    {
        get
        {
            int[] result = new int[currentZeroBased.Length];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = currentZeroBased[i] + lowerBounds[i];
            }
            return result;
        }
    }
}

And you can use it like this for setting the whole array:

int[,] myArray = new int[2, 2];
ArrayIndexer arrayIndexer = new ArrayIndexer(new[] {2, 2}, new[] {0, 0});
int i = 0;
while (arrayIndexer.MoveNext())
{
    myArray.SetValue(++i, arrayIndexer.Current);
}