【问题标题】:Find a list by its name C# [closed]按名称 C# 查找列表 [关闭]
【发布时间】:2013-03-24 12:30:02
【问题描述】:

我有一系列列表,我想创建一个方法,该方法将通过列表名称查找列表并返回列表。列表存储在类本身中。

    public void AddCord(string cord, string listName)
    {
        List<String> myShip;
        myShip = findListByName(listName);
        myShip.Add(cord);
    }

请保持代码使用最简单的方法..

【问题讨论】:

  • 你已经尝试了什么?
  • 也许您应该展示列表的存储方式以及名称的来源?是列表的名称,还是列表中船只的名称,或者您要搜索的内容?您是否已经尝试过实施它?如果是这样,那段代码是什么样的?
  • 嗨,对不起,如果我不清楚这些列表存储在类中,然后在类中构造如下: class Battleship { //Attributes, Player Ships private List _playerCarrierA;私有列表 _playerDestroyerA;公共战舰() { _playerCarrierA = new List(); _playerDestroyerA = new List();有比这更多的列表,基本上我想要一个方法来检索列表的名称,然后添加一个字符串到该列表。我试过很多方法,都没有成功。

标签: c# list


【解决方案1】:

试试这个:

//Create global dictionary of lists
Dictionary<string, List<string> dictionaryOfLists = new Dictionary<string, List<string>();

//Get and create lists from a single method
public List<string> FindListByName(string stringListName)
{
    //If the list we want does not exist yet we can create a blank one
    if (!dictionaryOfLists.ContainsKey(stringListName))
        dictionaryOfLists.Add(stringListName, new List<string>());

    //Return the requested list
    return dictionaryOfLists[stringListName];
}

【讨论】:

  • 哦,太棒了,这可能行得通 :D 谢谢我现在试试
【解决方案2】:
Dictionary<string, List<string>> myRecords=new Dictionary<string, List<string>>();

if(!myRecords.ContainsKey("abc"))
{
    List<string> abcList=new List<string>();
    myRecords.Add("abc", abcList);
}
else 
    myRecords.["abc"].Add("a");

【讨论】:

  • 非常感谢,以前从未遇到过字典 :)
【解决方案3】:

这是我的答案,在我眼中更有趣的一种解决方案:D

class MyList<T> : List<T>
{
    static List<object> superlist;

    public string name;

    ~MyList() {
        if (superlist != null)
        superlist.Remove(this);
    }

    public MyList(string name)
        : base() {
        init(name);
    }

    public MyList(string name, int cap)
        : base(cap) {
        init(name);
    }

    public MyList(string name, IEnumerable<T> IE)
        : base(IE) {
        init(name);
    }

    void init(string name) {
        if (superlist == null)
            superlist = new List<object>();

        this.name = name;
        superlist.Add(this);
    }

    public static void AddToListByName(T add, string listName) {
        for (int i = 0; i < superlist.Count; i++) {
            if (superlist[i].GetType().GenericTypeArguments[0] == add.GetType() && ((MyList<T>)(superlist[i])).name == listName) {
                ((MyList<T>)(superlist[i])).Add(add);
                return;
            }
        }
        throw new Exception("could not find the list");
    }

}

现在您可以在代码中轻松干净地使用它

        MyList<string> a = new MyList<string>("a");
        MyList<string> b = new MyList<string>("b");

        a.Add("normal add to list a");

        MyList<string>.AddToListByName("hello add to a", "a");
        MyList<string>.AddToListByName("hello add to b", "b");

【讨论】:

  • 我认为使用静态方法是一种巨大的代码气味。并且拥有析构函数真的会引发危险信号......
猜你喜欢
  • 2018-07-30
  • 2014-07-29
  • 2016-08-23
  • 1970-01-01
  • 2020-10-31
  • 2021-05-08
  • 2020-03-30
  • 1970-01-01
  • 2016-07-20
相关资源
最近更新 更多