C#遍历访问Dictionary和HashTable
来源:互联网 发布:u盘安装mac os 10.12 编辑:程序博客网 时间:2024/06/06 05:41
一般地Dictionary是按照元素的添加顺序输出的,这和HashTable不同。
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=xiaowangkey=21,value=dsdkey=33,value=dsfdfdkey=4,value=liusangkey=1,value=xiaowangkey=21,value=dsdkey=33,value=dsfdfdkey=4,value=liusangkey=21,value=xiao likey=4,value=fffkey=1,value=wang gangkey=33,value=xiao fanvalue=xiao livalue=fffvalue=wang gangvalue=xiao fankey=21,value=xiao likey=4,value=fffkey=1,value=wang gangkey=33,value=xiao fankey=21,value=xiao likey=4,value=fffkey=1,value=wang gangkey=33,value=xiao fan
Dictionary 通过索引指定下标访问指定键和值 || Dictionary_Value重新赋值
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重新赋值===========
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 终极用法
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; } }
本文转自网络,为两篇文章的合并:
http://hi.baidu.com/zkbob22/blog/item/d5aadfd26f96b7133bf3cf4d.html
http://www.cnblogs.com/blsong/archive/2010/08/18/1802382.html
- C#遍历访问Dictionary和HashTable
- C#遍历访问Dictionary和HashTable
- C#遍历访问Dictionary和HashTable
- C#遍历访问Dictionary和HashTable
- 遍历Dictionary和HashTable的方法
- c#中Dictionary、ArrayList、Hashtable和数组
- C# hashTable 和 Dictionary的区别。
- C# HashTable、HashSet和Dictionary的区别
- C# 的Hashtable和Dictionary比较
- C# 的Hashtable和Dictionary比较
- (转载)C# 的Hashtable和Dictionary比较
- Hashtable和Dictionary区别
- HashTable、HashSet和Dictionary
- 有关hashtable和dictionary
- Dictionary 和hashtable区别
- c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别
- C#中Dictionary、ArrayList、Hashtable和数组的区别是什么?
- c#中Dictionary、ArrayList、Hashtable和数组的区别
- Java5.0新特性06-泛型(Generic)
- CListCtrl使用技巧
- Silverlight调用一般性处理程序模拟Silverlight调用WCF效果(2)
- 在linux(或ubuntu)下如何杀死进程
- Mysql 创建联合主键
- C#遍历访问Dictionary和HashTable
- mysql创建唯一索引
- AxWindowsMediaPlayer使用
- Overload与Override的区别
- 多线程信号返回测试结果
- mysql创建线程安全客户端
- GIS资源网址收集
- 英文口语
- Matrix Power Series