【问题标题】:dotnet:is there any way out to directly get reference of value collection in dictionary?dotnet:有没有办法直接获取字典中值集合的引用?
【发布时间】:2010-10-27 21:23:02
【问题描述】:

我想根据每个字典项的值对字典进行排序。但是如果我使用排序字典搜索时间复杂度将从常量增加到 log2(n)。所以我想直接将字典中值列表的引用分配给列表.然后我可以对该列表进行排序并获得结果。我不想迭代字典的每个元素以将其值添加到列表中,这会增加复杂性?

【问题讨论】:

    标签: .net sorting collections dictionary


    【解决方案1】:

    您可以通过Dictionary<>.Values 属性获取值集合。在下面的示例中,您不会循环遍历值,但框架会为您完成。

    Dictionary<int, Item> items = new Dictionary<int, Item>();
    List<Item> values = new List<Item>(items.Values);
    values.Sort();
    

    您还可以为值保留另一个列表,您可以将项目添加到字典和列表中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-12
      • 2020-01-07
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2019-08-22
      相关资源
      最近更新 更多