如何在一行中将多个值加载到多维数组中

本文关键字:加载 数组 一行 | 更新日期: 2023-09-27 18:06:54

我在试

    SearchResultCollection src = searcher.FindAll();
    string[,] newLine = new string[src.Count, 4];
    foreach (SearchResult res in src)          
    {
        newLine[rowID, ] = new string[ , ] {"value1", "value2", "value3", "value4"};  //Syntax error; value expected

but not luck ->语法错误;

如何在一行中将多个值加载到多维数组中

根据OP的澄清,这样的东西可能会起作用。

string[ , ] newLine = new string[src.Count, 4];
for (int i = 0; i < src.Count; i++)
{
    newLine[i, 0] = value1;
    newLine[i, 1] = value2;
    newLine[i, 2] = value3;
    newLine[i, 3] = value4;
}

也许不是最漂亮的解决方案,但它可以完成工作。

看看Array.Copy

举个例子…

        string[,] newLine = new string[src.Count, 4];
        for (int i = 0; i < newLine.GetUpperBound(0); i++)
        {
            for (int j = 0; j < newLine.GetUpperBound(1); j++)
            {
                newLine[i, j] = "....";
            }
        }