简体   繁体   中英

Data structure that supports access via index as well as key

Dictionary supports access of elements via a key. List supports access via index. Is there any data structure that supports access via both key and index?

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("abc", "def");
d.Add("ghi", "ghi");
d.Add("abcd", "abcd");
d.Add("how", "howis");
foreach (KeyValuePair<string, string> kv in d)
   Console.WriteLine(kv.Key);

Output is

abc
ghi 
abcd 
how

So the items come out in the same sequence as they were added. Does that mean dictionary maintains items sequentially ? If yes, can we access them by index ? If no, is there any such collection available which supports accessing elements by index as well as key ?

我认为OrderedDictionary NameValueCollection 是您想要的。

Values in a dictionary are not necessarily unique, hence the key lookup.

The order of elements in a dictionary is non-deterministic, hence the OrderedDictionary .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM