Come si fa a iterare un array multidimensionale senza conoscere il numero di dimensioni ed elementi dell'array che vi vengono passati?

StackOverflow https://stackoverflow.com/questions/1627982

Domanda

Un SDK mi sta restituendo un array con più dimensioni come:

   int [,,] theArray = new int [2,8,12];

Devo visitare ogni elemento dell'array e restituire il valore e la posizione del valore. Devo farlo senza conoscere il numero di dimensioni ed elementi dell'array che viene passato.

È stato utile?

Soluzione

Qualcosa del genere funzionerebbe per te? Richiama i ranghi in modo da poter usare un foreach () e ottenere un array contenente gli indici dell'oggetto corrente.

class Program
{
    static void Main(string[] args)
    {
        int[, ,] theArray = new int[2, 8, 12];
        theArray[0, 0, 1] = 99;
        theArray[0, 1, 0] = 199;
        theArray[1, 0, 0] = 299;

        Walker w = new Walker(theArray);

        foreach (int i in w)
        {
            Console.WriteLine("Item[{0},{1},{2}] = {3}", w.Pos[0], w.Pos[1], w.Pos[2], i);
        }

        Console.ReadKey();
    }

    public class Walker : IEnumerable<int>
    {
        public Array Data { get; private set; }
        public int[] Pos { get; private set; }

        public Walker(Array array)
        {
            this.Data = array;
            this.Pos = new int[array.Rank];
        }

        public IEnumerator<int> GetEnumerator()
        {
            return this.RecurseRank(0);
        }

        private IEnumerator<int> RecurseRank(int rank)
        {
            for (int i = this.Data.GetLowerBound(rank); i <= this.Data.GetUpperBound(rank); ++i)
            {
                this.Pos.SetValue(i, rank);

                if (rank < this.Pos.Length - 1)
                {
                    IEnumerator<int> e = this.RecurseRank(rank + 1);
                    while (e.MoveNext())
                    {
                        yield return e.Current;
                    }
                }
                else
                {
                    yield return (int)this.Data.GetValue(this.Pos);
                }
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.RecurseRank(0);
        }
    }
}

Altri suggerimenti

Usa per loop:

for (int i=theArray.GetLowerBound(0);i<=theArray.GetUpperBound(0);++i)
{
    for (int j=theArray.GetLowerBound(1);j<=theArray.GetUpperBound(1);++j)
    {
        for (int k=theArray.GetLowerBound(2);k<=theArray.GetUpperBound(2);++k)
        {
           // do work, using index theArray[i,j,k]
        }
    }
}

Se non si conosce in anticipo il numero di dimensioni, è possibile utilizzare Array.Rank per determinarlo.

Non sono sicuro di aver compreso la tua domanda su " restituisci la posizione [n, n, n] " , ma se stai provando a restituire più di un valore da un metodo , ci sono un paio di modi per farlo.

& # 8226; Usa out o i parametri di riferimento (ad es. Int ) che vengono impostati sui valori restituiti prima di tornare dal metodo.

& # 8226; Passa un array, ad esempio un array di tre ints, i cui elementi vengono impostati dal metodo prima che ritorni.

& # 8226; Restituisce una matrice di valori, ad esempio una matrice di tre in.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top