Overly Excessive
Overly Excessive

Reputation: 2125

Referencing an element in an array inside a list

So I have created a function that returns a list of arrays, each array contains 3 cells. But I can't figure out how I would reference each element of the array that is residing inside of the list. Here is the code:

public List<Cell[]> GetEmptyRows()
    {
        var selection = new List<Cell[]>();
        selection.ForEach(entry => entry.Initialize()); // Not sure if this is necessary but let's keep it here for now

        for (int i = 0; i < this.cells.GetLength(0); i++)
        {
            var rows = new List<Cell[]>() { cells[i,i].HorizontalRelatives(this), cells[i,i].VerticalRelatives(this) };

            if (i == 1)
            {
                rows.Add(cells[i, i].DiagonalRelatives(this));
                rows.Add(cells[i, i].DiagonalRelatives2(this));
            }

            selection = rows.FindAll(array => array.Length.Equals(3));
        }

        return selection;
    }

Upvotes: 0

Views: 323

Answers (1)

ifloop
ifloop

Reputation: 8386

I'm not sure, if that is what you are looking for but, to reference a single element of one of your arrays, just access them via [].

public Cell GetCell(List<Cell[]> list, int row, int cell)
{
    if (list.Count < row || list[row].Length < cell)
        return;

    return list[row][cell];
}

That should do the trick.

Of cause, don't have to pass the target list as a paramater. You could place this function inside the class that holds you list.

Upvotes: 2

Related Questions