【问题标题】:Defining two dimensional Dynamic Array with different types定义不同类型的二维动态数组
【发布时间】:2009-12-21 10:56:03
【问题描述】:

我想创建不同类型的二维数组,比如我可以向该数组添加两个值,其中一个是控件名称,第二个是布尔值。

【问题讨论】:

  • 我不确定我是否理解您的问题,但字典不能满足您的要求吗?您是否尝试存储键/值对?
  • 使用带有元组的列表public static List<Tuple<string, Point>> pixelsArr = new List<Tuple<string, Point>>();

标签: c# .net windows


【解决方案1】:

你不能那样做。相反,您应该创建一个包含这两个属性的类,然后您可以创建该类型的数组:

public class MyClass
{
    public string ControlName {get;set;}
    public bool MyBooleanValue {get;set;}
}

public MyClass[] myValues=new MyClass[numberOfItems];

或者,正如 Anders 所说,如果要使用其中一个属性来执行查找,请使用字典。

【讨论】:

    【解决方案2】:

    字典将适用于您正在尝试做的事情。

    Dictionary<string, bool> controllerDictionary = new Dictionary<string, bool>();
    

    设置值

    if (controllerDictionary.ContainsKey(controllerName))
        controllerDictionary[controllerName] = newValue;
    else
        controllerDictionary.Add(controllerName, newValue);
    

    获取价值

    if (controllerDictionary.ContainsKey(controllerName))
        return controllerDictionary[controllerName];
    else
        //return default or throw exception
    

    【讨论】:

    • 你可以使用controllerDictionary[controllerName] = newValue;ContainsKeyAdd不是必需的
    • 是的,没错,只是get需要
    【解决方案3】:

    你不能用数组来做到这一点。

    也许您应该使用Dictionary

    Dictionary&lt;string,bool&gt; 的通用字典似乎适合您的描述。

    【讨论】:

      【解决方案4】:

      如果您想通过控件名称查找/设置布尔值,您可以使用Dictionary&lt;string, bool&gt;

      【讨论】:

        【解决方案5】:

        使用字典。 如果出于某种原因,您确实需要一个数组,请尝试 object[,] 并将其值转换为您想要的类型。

        【讨论】:

          【解决方案6】:

          另一种方法是创建一个对象类型的数组,然后将其添加到数组列表中。 下面是一些示例代码:

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using System.Collections;
              using System.Collections.Generic;
          
              namespace Collections
              {
                  class Program
                  {
                      static void Main(string[] args)
                      {
                          ArrayList ar = new ArrayList();
                          object[] o = new object[3];
                          // Add 10 items to arraylist
                          for (int i = 0; i < 10; i++)
                          {
                              // Create some sample data to add to array of objects of different types.
                              Random r = new Random();
                              o[0] = r.Next(1, 100);
                              o[1] = "a" + r.Next(1,100).ToString();
                              o[2] = r.Next(1,100);
                              ar.Add(o);
                          }
                      }
                  }
              }
          

          【讨论】:

            【解决方案7】:

            这取决于你想如何使用你的数组。您想通过键还是通过索引来查找值? Konamiman 建议上课。但是一个有两种类型的类只不过是一个Dictionary&lt;type of key, type of value&gt;。 如果要通过键获取值,可以使用字典。 像这样:

            Dictionary<string, int> MyDict = new Dictionary<string, int>();
            MyDict.Add("Brutus", 16);
            MyDict.Add("Angelina", 22);
            int AgeOfAngelina = MyDict["Angelina"];
            

            现在字典的缺点是,你不能迭代它。订单未定。您不能使用MyDict[0].Value 来获取Brutus 的年龄(即16 岁)。

            你可以用一个

            List<KeyValuePair<string, int>> MyList = new List<KeyValuePair<string, int>>();
            

            迭代两个不同类型的二维数组,因为List 支持迭代。但是话又说回来,你不能通过MyList["Angelina"].Value获得安吉丽娜的年龄,但你必须使用MyList[0].Value

            但您也可以使用数据表。但是用它的列来初始化表需要更多的工作。

            【讨论】:

            • 当然您可以使用 LINQ 进行扩展以对您的数据使用查询。
            • 你什么时候不能遍历字典了?
            【解决方案8】:

            "多维数组是一个数组: 所有维度中的所有元素都具有相同的类型”

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-12-11
              • 2018-01-16
              • 2020-07-10
              • 2023-04-06
              • 2016-03-18
              相关资源
              最近更新 更多