字符串表列表c#

字符串表列表c#,c#,C#,我的c#脚本有问题。我想创建一个字符串表列表,如下所示: public List<string[]> Signals = new List<string[]>(); public string[] Communication = new string[3]; foreach (string[] Signal in Signals) { foreach (string CommunicationUnit in Signal) { Pr

我的c#脚本有问题。我想创建一个字符串表列表,如下所示:

public List<string[]> Signals = new List<string[]>(); 
public string[] Communication  =  new string[3];
foreach (string[] Signal in Signals)
{
    foreach (string CommunicationUnit in Signal)
    {
        Print(CommunicationUnit);
    }
}
我将它们存储到我的列表中:

Signals.Add(Communication); // I have also tried .Insert with different Indexes
当我多次这样做,存储6个不同的通信表时,我希望最终看到我的每个通信实例,因此我执行以下操作:

public List<string[]> Signals = new List<string[]>(); 
public string[] Communication  =  new string[3];
foreach (string[] Signal in Signals)
{
    foreach (string CommunicationUnit in Signal)
    {
        Print(CommunicationUnit);
    }
}
我所能看到的输出是列表中最后一个元素的6倍,如下所示

ARINC3

121us

通信6

ARINC3

121us

通信6

ARINC3

121us

通信6

ARINC3

121us

通信6

ARINC3

121us

通信6

ARINC3

121us

通信6

当我想我会看到列表中的每个元素时,实际上是6个不同的字符串表


我不明白我的剧本怎么了。我认为错误在于foreach循环的某个地方。

在类Signal中创建一个包含通信列表的属性,并删除字符串数组


然后使用Signal类中的列表添加通信。

输出正确:前3个输出行是您的第一个表(或Signals.first())


ARINC3重新创建一个简单的示例:(插入后实例化一个新字符串数组时,它会起作用)

列表信号=新列表();
字符串[]通信=新字符串[3];
通讯[0]=“a”;
通信[1]=“b”;
通信[2]=“c”;
信号。添加(通信);
通信=新字符串[3];
通信[0]=“d”;
通信[1]=“e”;
通信[2]=“f”;
信号。添加(通信);
foreach(信号中的字符串[]信号)
{
foreach(信号中的字符串通信单元)
{
控制台写入线(通信单元);
}
}

您似乎只在列表中添加了6次相同的通信对象

如果在列表中存储对象,则只存储对该对象的引用,而不是副本

然后,如果您将第一个通信项添加到列表中,然后对其进行修改并再次添加,那么内存中对同一对象的相同引用只会增加2倍

这意味着,如果您更新了通讯项目,您只需更新列表中的所有会议

您必须为每个项目创建一个新的通信项目

Signals.Add(new string[3] {
    CommunicationType,
    CommunicationTime,
    CommunicationName
});
Signals.Add(new string[3] {
    CommunicationType2,
    CommunicationTime2,
    CommunicationName2
});


如果你使用同一个对象,你只会覆盖它的数据。

只要保持简单和干净,不要让它对你自己太难,它已经够难了

我只使用1个foreach循环,而不是2个,并且不创建多个列表。我只是有了清单,然后在我的课堂上反复讨论

List<string[]> Signals = new List<string[]>(); 
    string[] Communication  =  new string[3];

Communication[0] = "a";
Communication[1] = "b";
Communication[2] = "c";

Signals.Add(Communication);
i
foreach (string CommunicationUnit in Signals)
{
    Console.Writeline(CommunicationUnit[i]);

}
列表信号=新列表();
字符串[]通信=新字符串[3];
通讯[0]=“a”;
通信[1]=“b”;
通信[2]=“c”;
信号。添加(通信);
我
foreach(信号中的字符串通信单元)
{
Console.Writeline(通信单元[i]);
}

使用类而不是
string[]
。我的心理调试技能告诉我,你对所有6个使用相同的
通信
数组,因此它们在共享相同引用时都显示最后的数据。你最好按照别人的建议去做,创建一个类来建模你的数据结构。你没有展示出有问题的代码。显示使用通信阵列填充信号列表的完整代码。当您这样做时,很明显您并没有为每个列表元素创建新数组。相反,您正在用对同一数组的多次引用填充列表。为什么要在foreach中使用foreach?让通信单元在信号中循环会更容易,对吗?您也可以这样做
signals.SelectMany(x=>x).ToList().ForEach(Console.WriteLine)但我怀疑这会让他更容易理解。哈哈哈XD,但仍不明白为什么要列一个列表并将其放入字符串[]。我的意思是,你可以在列表中循环,这比2循环容易得多。这很可能是这里的情况。谢谢你向我解释这一点,但正如你所说的,如果每次我覆盖通信,我实际上会在我的所有列表中写入相同的数据,为什么每次插入后使用Communication=new string[3];重置通信时它会工作?因为当您使用“new”关键字时,它会创建一个新对象,然后创建一个新引用。但您的上一个对象仍在内存中,因为列表仍在使用它。如果你使用“沟通”,你就再也无法访问它了。你可以检查一下,或者如果你想要更多的信息。
string[] Communication  =  new string[3];
Communication[0]=CommunicationType;
Communication[1]=CommunicationTime;
Communication[2]=CommunicationName;
Signals.Add(Communication);
string[] Communication2  =  new string[3];
Communication2[0]=CommunicationType;
Communication2[1]=CommunicationTime;
Communication2[2]=CommunicationName;
Signals.Add(Communication2);
List<string[]> Signals = new List<string[]>(); 
    string[] Communication  =  new string[3];

Communication[0] = "a";
Communication[1] = "b";
Communication[2] = "c";

Signals.Add(Communication);
i
foreach (string CommunicationUnit in Signals)
{
    Console.Writeline(CommunicationUnit[i]);

}