【问题标题】:How to use list in this example在这个例子中如何使用列表
【发布时间】:2012-10-11 18:47:10
【问题描述】:

假设我有一个这样的数组(我知道这在 c# 上是不可能的):

string[,] arr = new string[,]
{
    {"expensive", "costly", "pricy", 0},
    {"black", "dark", 0}
};

那么我如何在列表中添加这些项目以及如何在 "pricy"0 之间添加新项目?我在网上找不到任何例子。

【问题讨论】:

标签: c# arrays list


【解决方案1】:

数组是不可变的,因此您不能真正添加​​或删除其中的项目。例如,您唯一可以做的就是将项目复制到另一个数组实例,减去您不想要的项目,或者执行相同但使用更高维度并添加您需要添加的项目。

我建议在此处使用List<T>,其中T 可能是一种简单的类型,可以反映您添加到数组中的内容。例如:

class Thing {
    public string Prop1 {get; set; }
    public string Prop2 {get; set; }
    public string Prop3 {get; set; }
    public int Prop4 {get; set; }
}

List<Thing> list = new List<Thing>();

list.Add(new Thing() { Prop1 = "expensive", Prop2 = "costly", Prop3 = "pricy", Prop4 = 0};

然后你可以插入项目:

list.Insert(1, new Thing() { Prop1 = "black", Prop2 = "dark", Prop4 = 0});

不确定这是否适合您,这取决于您的“锯齿状”数据是否可以适合Thing。显然,这里的“Prop1”等将是您在数组中拥有的数据的实际属性名称。

【讨论】:

    【解决方案2】:

    如果您想添加(插入)项目,请不要使用数组。使用List&lt;&gt;

    您的样本可能被

    覆盖
    var data = new List<string>[2] { new List<string>(), new List<string> () };
    

    然后您可以使用类似的语句

    data[0].Add("expensive");
    string s = data[1][1];     // "dark"
    

    在字符串数组或列表中当然不可能有0。您可以使用null,但请先避免使用它。

    【讨论】:

      【解决方案3】:

      那么你想要一个列表 OF 什么?现在你已经有了字符串和整数,所以object 是你的通用基类

      你可以做一个交错数组(数组的数组):

      object[][] arr = new []
      {
          new object[] {"expensive", "costly", "pricy", 0},
          new object[] {"black", "dark", 0}
      };
      

      或列表列表:

      List<List<object>> arr = new List<List<object>> 
      {
          new List<object> {"expensive", "costly", "pricy", 0},
          new List<object> {"black", "dark", 0}
      };
      

      但这两个似乎都是糟糕的设计。如果您提供有关您要完成的工作的更多信息,您可能会得到一些更好的建议。

      【讨论】:

        【解决方案4】:

        您可以将其设为Dictionary&lt;string,string&gt;,但密钥必须保持唯一。然后你就可以像这样循环了

        Dictionary<string,string> list = new Dictionary<string,string>();
        foreach(KeyValuePair kvp in list) 
        {
            //Do something here
        }
        

        【讨论】:

          【解决方案5】:

          你的任务有点奇怪,我不明白它在哪里有用。但是在您的上下文中,您可以在没有 List 的情况下执行此操作,依此类推。您应该按索引器遍历元素(在您的示例项目中,字符串 [,] 只能通过两个索引获取)。

          所以这里的解决方案有效,我这样做只是为了有趣

          var arr = new[,]
                                    {
                                        {"expensive", "costly", "pricy", "0"},
                                        {"black", "dark", "0", "0"}
                                    };
                      int line = 0;
                      int positionInLine = 3;
                      string newValue = "NewItem";
          
          
                      for(var i = 0; i<=line;i++)
                      {
                          for (int j = 0; j <=positionInLine; j++)
                          {
                              if (i == line && positionInLine == j)
                              {
                                  var tmp1 = arr[line, positionInLine];
                                  arr[line, positionInLine] = newValue;
          
                                  try
                                  {
                                      // Move other elements
                                      for (int rep = j+1; rep < int.MaxValue; rep++)
                                      {
                                          var tmp2 = arr[line, rep];
          
                                          arr[line, rep] = tmp1;
          
                                          tmp1 = tmp2;
          
                                      }
                                  }
                                  catch (Exception)
                                  {
          
                                      break;
                                  }
                              }
                          }
                      }
          

          【讨论】:

            【解决方案6】:
            class Program
            {
                static void Main(string[] args)
                {
                    List<Array> _list = new List<Array>();
            
                    _list.Add(new int[2] { 100, 200 });
                    _list.Add(new string[2] { "John", "Ankush" });
            
                    foreach (Array _array in _list)
                    {
                        if (_array.GetType() == typeof(Int32[]))
                        {
                            foreach (int i in _array)
                                Console.WriteLine(i);
                        }
                        else if (_array.GetType() == typeof(string[]))
                        {
                            foreach (string s in _array)
                                Console.WriteLine(s);
                        }
                    }
                    Console.ReadKey();
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-12-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多