转载---C#遍历访问Dictionary和HashTable--- http://blog.csdn.net/yan_hyz/article/details/7415221

dictionaryc#stringtableclasssystem
一般地Dictionary是按照元素的添加顺序输出的,这和HashTable不同。
[csharp] view plaincopy
Dictionary<int, string> dictionary = new Dictionary<int, string>();  
dictionary.Add(1, "xiaowang");  
dictionary.Add(21, "dsd");  
dictionary.Add(33, "dsfdfd");  
dictionary.Add(4, "liusang");   
  
foreach (KeyValuePair<int, string> kvp in dictionary)  
{  
    Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);  
}   
  
foreach (int j in dictionary.Keys)  
{  
    Console.WriteLine("key={0},value={1}", j, dictionary[j]);  
}   
  
Hashtable table = new Hashtable();   
  
table.Add(1, "wang gang");  
table.Add(21, "xiao li");  
table.Add(33, "xiao fan");  
table.Add(4, "fff");   
  
foreach (int i in table.Keys)  
{  
    Console.WriteLine("key={0},value={1}", i, table[i]);  
}   
  
foreach (string str in table.Values)  
{  
    Console.WriteLine("value={0}", str);  
}   
  
System.Collections.IDictionaryEnumerator d = table.GetEnumerator();  
while (d.MoveNext())  
{  
    System.Console.WriteLine("key={0},value={1}",d.Entry.Key,d.Entry.Value);  
}   
  
foreach (DictionaryEntry entry in table)  
{  
    Console.WriteLine("key={0},value={1}", entry.Key, entry.Value);  
}   
  
Console.ReadLine();   
  
    
  
输出结果:   
  
key=1,value=xiaowang  
key=21,value=dsd  
key=33,value=dsfdfd  
key=4,value=liusang  
key=1,value=xiaowang  
key=21,value=dsd  
key=33,value=dsfdfd  
key=4,value=liusang  
key=21,value=xiao li  
key=4,value=fff  
key=1,value=wang gang  
key=33,value=xiao fan  
value=xiao li  
value=fff  
value=wang gang  
value=xiao fan  
key=21,value=xiao li  
key=4,value=fff  
key=1,value=wang gang  
key=33,value=xiao fan  
key=21,value=xiao li  
key=4,value=fff  
key=1,value=wang gang  
key=33,value=xiao fan  

Dictionary 通过索引指定下标访问指定键和值 || Dictionary_Value重新赋值
[csharp] view plaincopy
Dictionary <int ,int > data = new Dictionary <int ,int >();   
data.Add(1,100);   
data.Add(2,100);   
data.Add(3,200);   
data.Add(4,100);   
   data.Keys.ElementAt(0);   
   data.Values.ElementAt(0);  
 
c# Dictionary 通过索引指定下标访问指定键和值
============================== 
=============  dictionary的遍历及赋值问题 ====Dictionary_Value重新赋值===========
[csharp] view plaincopy
Dictionary<int, string> datatable = new Dictionary<int, string>();  
datatable.Add(1, "hello 1");  
datatable.Add(2, "hello 2");  
int[] keys = datatable.Keys.ToArray();  
for (int i = 0; i < keys.Length; i++)  
{  
    if (datatable[keys[i]] == "hello 2")  
    {  
        datatable[keys[i]] = "hello";                     <<<<赋值方法为  
    }  
}  

Dictionary  终极用法
 
[csharp] view plaincopy
using System;     
using System.Collections.Generic;     
    
class DictionaryDemo     
{     
    static void Main(string[] args)     
    {     
        DictionaryDemo001();     
        Console.ReadLine();     
    
        DictionaryDemo002();     
        Console.ReadLine();     
    
        DictionaryDemo003();     
        Console.ReadLine();     
    }     
    
    /// <summary>     
    /// 一般用法     
    /// </summary>     
    static void DictionaryDemo001()     
    {     
        Dictionary<int, string> dict = new Dictionary<int, string>();     
        dict.Add(1, "111");     
        dict.Add(2, "222");     
    
        //判断是否存在相应的key并显示     
        if (dict.ContainsKey(2))     
        {     
            Console.WriteLine(dict[2]);     
        }     
    
        //遍历Keys     
        foreach (var item in dict.Keys)     
        {     
            Console.WriteLine("Key:{0}", item);     
        }     
    
        //遍历Values     
        foreach (var item in dict.Values)     
        {     
            Console.WriteLine("value:{0}", item);     
        }     
    
        //遍历整个字典     
        foreach (var item in dict)     
        {     
            Console.WriteLine("key:{0} value:{1}", item.Key, item.Value);     
        }     
    }     
    
    /// <summary>     
    /// 参数为其它类型     
    /// </summary>     
    static void DictionaryDemo002()     
    {     
        Dictionary<string, string[]> dict = new Dictionary<string, string[]>();     
        dict.Add("1", "1,11,111".Split(','));     
        dict.Add("2", "2,22,222".Split(','));     
        Console.WriteLine(dict["2"][2]);     
    }     
    
    /// <summary>     
    /// 调用自定义类     
    /// </summary>     
    static void DictionaryDemo003()     
    {     
        Dictionary<int, yongfa365> dict = new Dictionary<int, yongfa365>();     
        for (int i = 0; i < 10; i++)     
        {     
            yongfa365 y = new yongfa365();     
            y.UserCode = i;     
            y.UserName = "com " + i.ToString();     
            dict.Add(i, y);     
        }     
        foreach (var item in dict)     
        {     
            Console.WriteLine("{0} One:{1} UserName:{2}", item.Key, item.Value.UserCode, item.Value.UserName);     
        }     
    }     
}     
    
class yongfa365     
{     
    public int UserCode { get; set; }     
    public string UserName { get; set; }     
    
}    

 

posted @ 2015-08-14 11:36  BetterCoder  阅读(382)  评论(0编辑  收藏  举报