【问题标题】:Multi string array to multi dictionary in C#?C#中的多字符串数组到多字典?
【发布时间】:2022-01-16 13:02:55
【问题描述】:

我无法将以下数组更改为字典。

 public static string[][,] patterns = new string[][,]
 {
      new string[,] {
           { "1,2,3" },
           { "3,2,5" },
      },

      new string[,] {
           { "4,4,3" },
           { "7,1,2" },
      },
 
 };

这就是我所拥有的:

 public Dictionary<string, string[]> patterns = new Dictionary<string, string[]>();

我无法用预定义的值填充数组。

我想换成字典,因为它有一个键。

我也可以将上述数组更改为键值格式吗?

我想要这样的东西:{ "keyNameExample1", "1,2,3", "4,5,6", "etc"}。我想做这样的事情:patterns["keyNameExample", 1 (integer array pack)];patterns["keyNameExample", 2];(获取第二个数组)

{ "keyNameExample1", "1,2,3", "4,5,6", "etc"} { "keyNameExample2", "5,7,8", "1,1,1", "etc"} 并得到它:patterns["keyNameExample1", 2]; or patterns["keyNameExample2", 1];

【问题讨论】:

  • 字典应该有哪些键?您能否提供给定示例所需的字典?
  • 我想要这样的事情:{ "keyNameExample1", "1,2,3", "4,5,6", "etc"} 我想做这样的事情:patterns["keyNameExample ", 1 (整数数组包)];或模式["keyNameExample", 2]; (获取第二个数组)
  • { "keyNameExample1", "1,2,3", "4,5,6", "etc"} { "keyNameExample2", "5,7,8", "1,1 ,1", "etc"} 并像这样得到它:patterns["keyNameExample1", 2];或模式["keyNameExample2", 1];

标签: c# arrays dictionary


【解决方案1】:

可以让它更短,例如:

public static Dictionary<string, string[]> demo = new Dictionary<string, string[]>
            {
                { "abc", new[]{"1","2"}},
                { "def", new[]{"3","4"}},
            };

使用 C# 9,您甚至可以这样做:

public static Dictionary<string, string[]> demo = new()
            {
                { "abc", new[]{"1","2"}},
                { "def", new[]{"3","4"}},
            };

【讨论】:

    【解决方案2】:

    你可以把它变成一个带有列表的字典,我想这可以满足你的要求(索引访问、索引元素的可变整数个数)。这是一个示例(对于值列表,我不确定,您是否真的想要一个字符串或整数,以防只是更改类型):

    // define dictionary    
    IDictionary<string, IList<int>> dict = new Dictionary<string, IList<int>>();
        
    // assign values
    dict["abc"] = new List<int> { 2, 4, 8 };
    dict["def"] = new List<int> { 10, 12, 14 };
    
    // get value    
    int dictDef2 = dict["def"][1];
    

    【讨论】:

      【解决方案3】:

      终于明白了。

      public static Dictionary<string, string[]> demo = new Dictionary<string, string[]>
      {
          { "abc",
              new string[]
                  {
                  "1", 
                  "2"
                  }
          },
      
          { "def",
              new string[]
                  {
                  "3",
                  "4"
                  }
          },
      };
      

      包含检查:

      if (demo.ContainsKey("abc"))
      {
      }
      

      获取值:

      demo["abc"][0]
      

      感谢您的帮助。

      【讨论】:

        【解决方案4】:

        如果你想改变现有的数组,你可以试试Linq

        using System.Linq;
        
        ...
        
        public static string[][,] patterns = new string[][,] {...}
        
        ...
        
        public Dictionary<string, string[]> patternsDict = patterns
          .Select((value, index) => (
             key : $"keyNameExample{index}", 
             value : value.OfType<string>().ToArray()))
          .ToDictionary(pair => pair.key, pair => pair.value);
        

        注意,我们必须将 2d 数组转换(展平)为序数数组

        如果你只想声明字典并填写:

        public Dictionary<string, string[]> patterns = new Dictionary<string, string[]>() {
          {"keyExample1", new string[] { "1,2,3", "3,2,5" }},
          {"keyExample1", new string[] { "4,4,3", "7,1,2" }},
        };
        

        【讨论】:

          【解决方案5】:
          public string[][,] patterns = new string[][,] { new string[,] { { "1,2,3" }, { "3,2,5" } }, new string[,] { { "4,4,3" }, { "7,1,2" } } };
          
          Dictionary<string, string[,]> patternsDictionary = new Dictionary<string, string[,]>();
          
                      for (int i = 0; i < patterns.Length; i++)
                      {
                          patternsDictionary.Add(i.ToString(), patterns[i]);
          
                      }
                     
                      Console.WriteLine(patternsDictionary["0"][0,1]);  // Returns  2 - from { "1,2,3" }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-10-30
            • 2014-04-05
            • 1970-01-01
            • 2011-08-16
            • 1970-01-01
            • 2014-04-18
            • 2013-11-18
            相关资源
            最近更新 更多