当我添加一个新的项目列表<名单,LT;串GT;>父列表的每个项目获得的值相同 [英] When I add a new item to a List<List<string>> each item of the parent list get the same values

查看:112
本文介绍了当我添加一个新的项目列表<名单,LT;串GT;>父列表的每个项目获得的值相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道歉,如果这个问题的答案是显而易见的,我是相当新的C#和OOP。我已经加强,虽然我的代码,花了很长一段时间在谷歌,但我无法找到答案,我的问题(很可能是因为我使用了错误的搜索条件!)。



我有一个创建一个静态列表<下面的类;清单<串>> 并有一个方法项目添加到这个列表:

 公共静态类单词表
{
静态列表<名单<串> > _单词表; //静态列表例如

静态词库()
{
//
//分配名单。
//
_WordList =新的List<名单,LT;串GT;>();
}

公共静态无效的记录(列表<串>字)
{
//
//记录这个列表中的数值。
//
_WordList.Add(字);
}
}

在这里我创建

否则一个清单<串> 我传递到记录()方法被添加到 _WordList 。问题是,当我将项目添加到词库它给每一个项目在该列表中的值相同。例如:添加



1项包含富和栏



第二项补充含有不 富和栏



因此,而不是一个列表如下:

  1:富,酒吧
2:不是,富,酒吧

我结束了:

  1:不是,富, 酒吧
2:不是,富,酒吧

我没有使用列表<的代替列表<字符串[]>列表与LT;串>> ,因为顺便我得到了列表<串> 补充的是,通过阅读文本文件一行行用分隔符说时,我应该添加名单,LT ;串> 和明确的,所以我可以重新开始。因此,我不知道一个数组,我需要多长时间才能申报。



希望这是某种感觉!如果你需要的代码张贴了帮助让我知道。



谢谢,在前进。



修改



下面是创建的列表与LT码;串> 即传递到记录()方法。我想,我看到人们在说什么关于未创建列表℃的新实例;串> ,但我不知道如何问候我的代码解决这个问题。我将有一个想一想,然后再发布答案,如果我拿出一个!



 公共静态无效LoadWordList(字符串路径) 
{
串线;
名单,LT;字符串> WordsToAdd =新的List<串GT;();

StreamReader的文件=新就是System.IO.StreamReader(路径);
而((线= file.ReadLine())!= NULL)
{
如果(line.Substring(0,1)==$)
{
WordList.Record(WordsToAdd);
WordsToAdd.Clear();
WordsToAdd.Add(line.Replace($,));
}
,否则
{
WordsToAdd.Add(line.Replace(_,));
}
}
file.Close();
}


解决方案

所有你的录制方法做的是增加了列表℃的参考;串> 您已经传递给它。 。然后,您明确指出同一名单,并开始添加不同的字符串到它



也许是这样的:

 公共静态无效的记录(IEnumerable的<串GT;字)
{
_WordList.Add(Words.ToList());
}



这将迫使副本发生;同时,通过接受的IEnumerable<字符串方式> ,提出在调用它的代码少限制


Apologies if the answer to this is obvious, I'm fairly new to C# and OOP. I've stepped though my code and spent quite some time on Google but I can't find the answer to my question (quite possibly because I am using the wrong search terms!).

I have the following class that creates a static List<List<string>> and has a method to add items to that list:

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);
    }
}

Else where I create a List<string> which I pass into the Record() method to be added to _WordList. The problem is when I add items to WordList it gives every item in that list the same value. e.g.:

1st item added contains "Foo" and "bar"

2nd item added contains "Not","Foo" and "bar"

So instead of a list that looks like:

1: "Foo","bar"
2: "Not","Foo","bar"

I end up with:

1: "Not","Foo","bar"
2: "Not","Foo","bar"

I haven't used a List<string[]> instead of a List<List<string>> because the way I am getting the List<string> to add is by reading a text file line by line with a delimiter saying when I should add the List<string> and clear it so I can start again. Therefore I don't know how long an array I need to declare.

Hope this makes some kind of sense! If you need anymore of the code posting to help let me know.

Thanks, in advance.

EDIT

Here is the code for the creation of the List<string> that is passed to the Record() method. I think I see what people are saying about not creating a new instance of the List<string> but I'm not sure how to remedy this in regards to my code. I will have a think about it and post an answer if I come up with one!

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();
    }

解决方案

All that your Record method is doing is adding a reference to the List<string> you've passed to it. You then clear that same list, and start adding different strings to it.

Maybe something like:

public static void Record(IEnumerable<string> Words)
{
    _WordList.Add(Words.ToList());
}

Which will force a copy to occur; also, by accepting IEnumerable<string>, it puts less restrictions on the code that calls it.

这篇关于当我添加一个新的项目列表&LT;名单,LT;串GT;&GT;父列表的每个项目获得的值相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆