Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c-添加到字符串[]成员_C#_Asp.net_Linq_Visual Studio - Fatal编程技术网

C# c-添加到字符串[]成员

C# c-添加到字符串[]成员,c#,asp.net,linq,visual-studio,C#,Asp.net,Linq,Visual Studio,我有一个名为lst的列表: [0] "ABC" "DEF" "GHI" [1] "JKL" "MNO" "PQR" [2] etc, etc... 如何在每个lst成员的末尾添加另一个字符串 字符串s=EndOfBlock [0] "ABC" "DEF" "GHI" "EndOfBlock" [1] "JKL" "MNO" "PQR" "EndOfBlock" [2] etc, etc... 谢谢。应该是这样的: var lst = new List<string[]>(); l

我有一个名为lst的列表:

[0] "ABC" "DEF" "GHI"
[1] "JKL" "MNO" "PQR"
[2] etc, etc...
如何在每个lst成员的末尾添加另一个字符串

字符串s=EndOfBlock

[0] "ABC" "DEF" "GHI" "EndOfBlock"
[1] "JKL" "MNO" "PQR" "EndOfBlock"
[2] etc, etc...

谢谢。

应该是这样的:

var lst = new List<string[]>();
lst.Add(new string[] { "ABC", "DEF" });
lst.Add(new string[] { "GHI", "JKL" });

foreach (var item in lst)
{
    Console.WriteLine(item.Length);
}

for (int i = 0; i < lst.Count; ++i)
{
    var array = lst[i];
    Array.Resize(ref array, array.Length + 1);
    array[array.Length - 1] = "EndOfBlock";
    lst[i] = array;
}

foreach (var item in lst)
{
    Console.WriteLine(item.Length);
}

应该是这样的:

var lst = new List<string[]>();
lst.Add(new string[] { "ABC", "DEF" });
lst.Add(new string[] { "GHI", "JKL" });

foreach (var item in lst)
{
    Console.WriteLine(item.Length);
}

for (int i = 0; i < lst.Count; ++i)
{
    var array = lst[i];
    Array.Resize(ref array, array.Length + 1);
    array[array.Length - 1] = "EndOfBlock";
    lst[i] = array;
}

foreach (var item in lst)
{
    Console.WriteLine(item.Length);
}

我还建议您使用以下列表:


我还建议您使用以下列表:


正如评论者所指出的,听起来你真的想要一个列表。但是如果你想继续使用一个列表,下面是我如何扩展每个数组的

List<string[]> list = ...
for(int i = 0; i < list.Count; i++)
{
    // copy to a local variable (list[i] is a property, which can't be passed 'ref')
    string[] array = list[i];

    // resize the local variable. (this creates a new string[] object)
    Array.Resize(ref array, array.Length + 1);
    array[array.Length - 1] = "EndOfBlock";

    // put the new object back in the list, replacing the old smaller one.
    list[i] = array;
}

正如评论者所指出的,听起来你真的想要一个列表。但是如果你想继续使用一个列表,下面是我如何扩展每个数组的

List<string[]> list = ...
for(int i = 0; i < list.Count; i++)
{
    // copy to a local variable (list[i] is a property, which can't be passed 'ref')
    string[] array = list[i];

    // resize the local variable. (this creates a new string[] object)
    Array.Resize(ref array, array.Length + 1);
    array[array.Length - 1] = "EndOfBlock";

    // put the new object back in the list, replacing the old smaller one.
    list[i] = array;
}

如果你真的想用IList做这件事你可以这样做

var lst = new List<string[]>
{
    { "ABC", "DEF", "GHI" },
    { "JKL", "MNO", "PQR" },
    ...
}

var blockEnd = new string[] { "EndOfBlock" };

lst = lst.Select(a => 
    (a.Select(s => s).Concat(blockEnd))
    .ToArray()).ToList();

如果你真的想用IList做这件事你可以这样做

var lst = new List<string[]>
{
    { "ABC", "DEF", "GHI" },
    { "JKL", "MNO", "PQR" },
    ...
}

var blockEnd = new string[] { "EndOfBlock" };

lst = lst.Select(a => 
    (a.Select(s => s).Concat(blockEnd))
    .ToArray()).ToList();

如果要保留阵列列表,可以执行以下操作:

List<string[]> lst = new List<string[]); //Where this is the poplated list instead of new;
for (ini i = 0; i < lst.Count; i++)
{
   List<string> tmpList = new List<string>(lst[i]);
   tmpList.Add("EndOfBlock");
   lst[i] = tmpList.ToArray();
}

如果要保留阵列列表,可以执行以下操作:

List<string[]> lst = new List<string[]); //Where this is the poplated list instead of new;
for (ini i = 0; i < lst.Count; i++)
{
   List<string> tmpList = new List<string>(lst[i]);
   tmpList.Add("EndOfBlock");
   lst[i] = tmpList.ToArray();
}


如果要添加项目,请使用List/ListOf ListOf字符串。数组的大小是固定的。听起来你真的想要一个列表如果你想添加项目,请使用List/ListOf-ListOf-String。数组的大小是固定的。听起来你真的想要一个ListArray。Resize将创建一个新数组并将元素复制到其中。它不会将新的较大数组放回列表中。此方法分配具有指定大小的新数组,将元素从旧数组复制到新数组,然后用新数组替换旧数组。它通过引用传递,因此它也应该在列表中更新。局部变量数组得到更新,但不是原始列表。不超过array=null将影响列表。我收回这一点,@DavidYaw是100%正确的,这将不起作用。@DavidYaw,修复了我的代码。谢谢你!综上所述,如果我是用户1481183,我会改变我的数据类型。列表,或者类似的东西更适合使用。Array.Resize将创建一个新数组并将元素复制到其中。它不会将新的较大数组放回列表中。此方法分配具有指定大小的新数组,将元素从旧数组复制到新数组,然后用新数组替换旧数组。它通过引用传递,因此它也应该在列表中更新。局部变量数组得到更新,但不是原始列表。不超过array=null将影响列表。我收回这一点,@DavidYaw是100%正确的,这将不起作用。@DavidYaw,修复了我的代码。谢谢你!综上所述,如果我是用户1481183,我会改变我的数据类型。使用List或类似的东西会更好。但我不建议使用List.ForEach进行查找。我认为在这种情况下,使用List.ForEach比使用for循环更好,特别是因为每次迭代只有一条指令。我正在努力证明我的第一条评论是正确的,所以我认为你是对的。但我不会建议使用List.ForEach。我认为在这种情况下,使用List.ForEach比使用for循环更好,特别是因为每次迭代只有一条指令。我正在努力证明我的第一条评论是正确的,所以我认为你是对的。这回答了最初的问题。谢谢,这回答了原来的问题。非常感谢。