C# 将新项目添加到列表时<;列表<;字符串>&燃气轮机;父列表的每个项都获得相同的值
抱歉,如果答案很明显,我对C#和OOP相当陌生。我浏览了我的代码,在谷歌上花了不少时间,但我找不到问题的答案(很可能是因为我使用了错误的搜索词!) 我有一个类,它创建了一个静态的C# 将新项目添加到列表时<;列表<;字符串>&燃气轮机;父列表的每个项都获得相同的值,c#,generic-list,C#,Generic List,抱歉,如果答案很明显,我对C#和OOP相当陌生。我浏览了我的代码,在谷歌上花了不少时间,但我找不到问题的答案(很可能是因为我使用了错误的搜索词!) 我有一个类,它创建了一个静态的列表,并有一个向该列表添加项目的方法: public static class WordList { static List<List<string>> _WordList; // Static List instance static WordList() {
列表
,并有一个向该列表添加项目的方法:
public static class WordList
{
static List<List<string>> _WordList; // Static List instance
static WordList()
{
//
// Allocate the list.
//
_WordList = new List<List<string>>();
}
public static void Record(List<string> Words)
{
//
// Record this value in the list.
//
_WordList.Add(Words);
}
}
我的结局是:
1: "Not","Foo","bar"
2: "Not","Foo","bar"
我没有使用列表
而不是列表
,因为我获取要添加的列表
的方法是逐行读取文本文件,其中带有一个分隔符,告诉我应该在什么时候添加列表
,然后清除它,这样我就可以重新开始。因此,我不知道需要声明一个数组多长时间
希望这有点道理!如果你需要更多的代码张贴来帮助我,请告诉我
提前谢谢
编辑
下面是创建传递给Record()
方法的列表的代码。我想我看到了人们关于不创建列表的新实例的说法,但我不知道如何在我的代码中纠正这一点。我会想一想,如果我有一个答案,我会发布一个
public static void LoadWordList(string path)
{
string line;
List<string> WordsToAdd = new List<string>();
StreamReader file = new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
if (line.Substring(0, 1) == "$")
{
WordList.Record(WordsToAdd);
WordsToAdd.Clear();
WordsToAdd.Add(line.Replace("$", ""));
}
else
{
WordsToAdd.Add(line.Replace("_"," "));
}
}
file.Close();
}
publicstaticvoidloadwordlist(字符串路径)
{
弦线;
List WordsToAdd=新列表();
StreamReader file=new System.IO.StreamReader(路径);
而((line=file.ReadLine())!=null)
{
如果(行子字符串(0,1)=“$”)
{
WordList.Record(WordsToAdd);
WordsToAdd.Clear();
WordsToAdd.Add(line.Replace(“$”,”);
}
其他的
{
WordsToAdd.Add(line.Replace(““,”);
}
}
file.Close();
}
你能发布添加列表的代码吗?我打赌你在做类似的事情
创建一个列表l
加上
修改l
加上
这将导致一个对象(因为您只创建了一次)具有多个引用,即从_WordList中的第一个值,从_WordList中的第二个值,从l
因此,正确的做法是:
创建列表l
加上
创建新列表
加上
或在代码中:
List<string> l = new string[] { "Foo", "bar" }.ToList();
WordList.Record(l);
l = new string[] { "Not", "Foo", "bar" }.ToList();
WordList.Record(l);
List l=新字符串[]{“Foo”,“bar”}.ToList();
词表记录(l);
l=新字符串[]{“Not”,“Foo”,“bar”}.ToList();
词表记录(l);
您尚未显示如何将项目添加到列表中。下面是一个按预期工作的示例:
using System;
using System.Collections.Generic;
using System.Linq;
public static class WordList
{
static List<List<string>> _WordList; // Static List instance
static WordList()
{
_WordList = new List<List<string>>();
}
public static void Record(List<string> Words)
{
_WordList.Add(Words);
}
public static void Print()
{
foreach (var item in _WordList)
{
Console.WriteLine("-----");
Console.WriteLine(string.Join(",", item.ToArray()));
}
}
}
class Program
{
static void Main()
{
WordList.Record(new[] { "Foo", "bar" }.ToList());
WordList.Record(new[] { "Not", "Foo", "bar" }.ToList());
WordList.Print();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共静态类词表
{
静态列表_WordList;//静态列表实例
静态单词表()
{
_WordList=新列表();
}
公共静态无效记录(列表字)
{
_单词列表。添加(单词);
}
公共静态无效打印()
{
foreach(单词列表中的变量项)
{
Console.WriteLine(“----”);
Console.WriteLine(string.Join(“,”,item.ToArray());
}
}
}
班级计划
{
静态void Main()
{
WordList.Record(新[]{“Foo”,“bar”}.ToList());
WordList.Record(新的[]{“Not”,“Foo”,“bar}.ToList());
Print();
}
}
您的记录
方法所做的就是向传递给它的列表添加一个引用。然后清除相同的列表,并开始向其中添加不同的字符串
可能是这样的:
public static void Record(IEnumerable<string> Words)
{
_WordList.Add(Words.ToList());
}
公共静态无效记录(IEnumerable Words)
{
_WordList.Add(Words.ToList());
}
这将迫使复制发生;此外,通过接受IEnumerable
,它对调用它的代码的限制更少。而不是
WordList.Record(WordsToAdd);
WordsToAdd.Clear();
WordsToAdd.Add(line.Replace("$", ""));
做
WordList.Record(WordsToAdd);
WordsToAdd=新列表();
WordsToAdd.Add(line.Replace(“$”,”);
看起来您正在重用内部循环中的同一个列表
实例,可能偶尔会清除它。由于List
是一种引用类型,您需要为数组中的每个项创建一个唯一的实例,否则所有项最终都会引用同一个列表。Backticks(`)是将代码内联到问题中的好方法,并且可以避免使用,等等。谢谢,我明白您的意思。我用创建要传递的列表的代码编辑了我的帖子,正如你所看到的,我每次都使用相同的列表。我想我需要弄清楚每次如何创建一个新的列表。谢谢,这非常有效。感谢所有留下答案或评论的人。非常感谢。
WordList.Record(WordsToAdd);
WordsToAdd.Clear();
WordsToAdd.Add(line.Replace("$", ""));
WordList.Record(WordsToAdd);
WordsToAdd = new List<string>();
WordsToAdd.Add(line.Replace("$", ""));