zoukankan      html  css  js  c++  java
  • C#遍历Dictionary

    C#遍历Dictionary方法

    Dictionary<string, int> d = new Dictionary<string, int>();
    
    foreach (KeyValuePair<string, int> pair in d)
    {
    
         Console.WriteLine("{0}, {1}",
                    pair.Key,
                    pair.Value);
            }
    
    Dictionary<string, int> list = new Dictionary<string, int>();
                //C#3.0以上版本
    
                foreach (var item in list)
                {
                    Console.WriteLine(item.Key + item.Value);
                }
    
                //KeyValuePair<T,K>
                foreach (KeyValuePair<string, int> kv in list)
                {
                    Console.WriteLine(kv.Key + kv.Value);
                }
    
                //通过键的集合取
    
                foreach (string key in list.Keys)
                {
                    Console.WriteLine(key + list[key]);
                }
    
                //直接取值
                foreach (int val in list.Values)
                {
                    Console.WriteLine(val);
                } 
    
                //非要采用for的方法也可
                List<string> test = new List<string>(list.Keys);
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(test[i] + list[test[i]]);
    
                }
    
    IDictionary dictionary = new Hashtable();
    foreach (DictionaryEntry entry in dictionary)
    {
       Object key = entry.Key;
        Object val = entry.Value;
    }/* 何问起 hovertree.com */

    推荐:http://www.cnblogs.com/roucheng/p/3521864.html

  • 相关阅读:
    UVa 727
    UVa 11495
    UVa 299
    UVa 10194
    UVa 146
    10025
    Tug of War POJ 2576 DP(类似背包)
    Problem A: Freckles UVA 10034 裸生成树
    UVA 562
    CF DIV 2 206 C. Vasya and Robot
  • 原文地址:https://www.cnblogs.com/roucheng/p/csDictionary.html
Copyright © 2011-2022 走看看