0

我正在使用以下代码创建一个数组来找出多维数组的开始和结束索引:

var valueArray = Enumerable.Range(0, val.GetLength(1))
                                     .Where(index => val[0, index].Contains("Rak"))
                                     .ToArray();

比我使用 valueArray 来获取 valueArray [0] 的起始索引和 valueArray [valueArray.Length-1] 的结束索引。

上面的代码有效。但看起来它正在做大量的工作来获取开始和结束索引。因为它首先创建 valueArray 并将所有值放入其中,然后获取索引值。任何人都可以提出更好的方法吗?

数组的定义:

private string[,] _val;
 public string[,] val
        {
            [Pure]
            get
            {
                if (_val != null)
                    return (string[,])_val.Clone();
                return null;
            }
            set
            {
                _val = value;
            }
        }
4

2 回答 2

2

I'd rather would go for something like this:

public class CellInfo
{
    public int Row { get; set; }
    public int Column { get; set; }
    public string Value { get; set; }
}

Example:

List<CellInfo> _cells = new List<CellInfo>();

public void Example()
{
    _cells.Add(new CellInfo { Column = 6, Row = 3, Value = "Rak" });
    _cells.Add(new CellInfo { Column = 3, Row = 8, Value = "Rak" });
    _cells.Add(new CellInfo { Column = 2, Row = 4, Value = "Rak" });
    _cells.Add(new CellInfo { Column = 5, Row = 7, Value = "Sac" });
    _cells.Add(new CellInfo { Column = 1, Row = 3, Value = "Sac" });

    int[] rowRakAtColumn0 = _cells.Where(cell => cell.Column == 0
                                        && cell.Value.Contains("Rak"))
                                            .OrderBy(cell => cell.Row)
                                                .Select(cell => cell.Row)
                                                    .ToArray();

    CellInfo[] row0Values = _cells.Where(cell => cell.Row == 0)
                                .OrderBy(cell => cell.Column)
                                    .ToArray();
}

Converting it to a multi array (if needed)

public string[,] ToMultiArray(List<CellOffset> cells)
{
    string[,] multiDimentional = new string[
        cells.Max(cell => cell.Column), cells.Max(cell => cell.Row)];

    cells.ForEach(cell => 
        multiDimentional[cell.Column, cell.Row] = cell.Value);

    return multiDimentional;
}

string[,] values = ToMultiArray(_cells);

Answer to lazzy_ms:

The linq .Select() method is also able to pass the index as second parameter for your lambda function. You only need to capture it in an anonymous class.

var result = _cells
    //             here         and capture it in an object
    //              |
    .Select((Item, Index) => new { Item, Index })
    .FirstOrDefault(itemWithIndex => itemWithIndex.Item.Column == 5);

Console.WriteLine($"The index of column 5 is {result?.Index}");

Make sure you select the index before any filtering takes place. else it wont match

于 2013-10-04T20:19:07.897 回答
0

优化代码的一种方法是分别从开头和结尾搜索第一个和最后一个元素。

int firstIndex;
int lastIndex; 
for(int i = 0; i < val.GetLength(1); i++)
{
    if(val[0,i].Contains("Rak"))
    {
        firstIndex = i;
        break;
    } 
}
for(int i = val.GetLength(1) - 1; i>=0; i--)
{
    if(val[0,i].Contains("Rak"))
    {
        lastIndex = i;
        break;
    } 
}
于 2013-10-04T19:39:28.360 回答