【问题标题】:print OrderedDictionary array values?打印 OrderedDictionary 数组值?
【发布时间】:2017-04-05 06:42:26
【问题描述】:

Line:从文件中获取一行,它是迭代

string[] values = line.split(","); // ex: ["hi, "test", "no", "sup"]

OrderedDictionary od = new OrderedDictionary();
Tuple<int, string[]> innerTuple = new Tuple<int, string[]>(int.Parse(value[0], values);

我似乎找不到打印值数组的方法。但是,我可以使用常规字典打印出数组(我不喜欢字典如何混淆插入顺序)。

【问题讨论】:

  • 当您使用Dictionary 时如何打印出数组?当您尝试使用 OrderedDictionary 执行此操作时会发生什么?
  • 使用其他数据结构...
  • @Servy 使用字典,我使用 foreach 来获取键,然后使用该键为每个 value[index] 执行 forloop,例如:Console.WriteLine(dict[k][index])。我试图在 OrderedDictionary 中做到这一点,没有线索。
  • @user7190976 你在使用OrderedDictionary 时遇到了什么问题?
  • @Servy 我尝试使用foreach(var k in od.keys) { for(int index = 0; index &lt; od[k]. ; index++),然后在od[k] 之后放置一个点时,它只显示我正在寻找长度或计数的“Equals、GetHashCode、GetType 或 ToString”。

标签: c# ordereddictionary


【解决方案1】:

试试这个:

foreach (DictionaryEntry item in od)
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value);
}

更新:

如果你有一个字符串数组作为值,那么你应该尝试这样的事情:

foreach (DictionaryEntry item in od)
{
    if (item.Value is string[])
    {
        foreach (string str in (string[])item.Value)
        {
            Console.WriteLine("A string item: " + str);
        }
    }
}

【讨论】:

  • 这行不通。 Item.value 将返回 string[].
  • 谢谢!这就是我一直在寻找的。​​span>
  • 好的,我明白了。那么对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 2020-08-25
  • 2018-03-11
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
相关资源
最近更新 更多