如何在字典中检索第N个项目? [英] How to retrieve Nth item in dictionary?

查看:103
本文介绍了如何在字典中检索第N个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何从字典中获得第n个元素?

Possible Duplicate:
How do I get the nth element from a Dictionary?

如果一个Dictionary总共有Y个项目,而当N<时我们需要第N个项目. Y那么如何实现呢?

If there's a Dictionary with total of Y items and we need Nth item when N < Y then how to achieve this?

示例:

Dictionary<int, string> items = new Dictionary<int, string>();

items.add(2, "Bob");
items.add(5, "Joe");
items.add(9, "Eve");

// We have 3 items in the dictionary.
// How to retrieve the second one without knowing the Key?

string item = GetNthItem(items, 2);

如何编写GetNthItem()?

推荐答案

字典未排序.没有第n个项目.

Dictionary isn't ordered. There is no nth item.

使用OrderedDictionary和Item()

Use OrderedDictionary and Item()

这篇关于如何在字典中检索第N个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆