c#中directory的基本用法
1、创建及初始化 Dictionary<int, string> myDictionary = new Dictionary<int, string>(); 2、添加元素 myDictionary.Add("C#",0); myDictionary.Add("C++",1); myDictionary.Add("C",2); myDictionary.Add("VB",2); 3、查找元素By Key java中式调用方法keyset放入set集合中,然后利用Iterator迭代器遍历SET if(myDictionary.ContainsKey("C#")) { Console.WriteLine("Key:{0},Value:{1}", "C#", myDictionary["C#"]); } 4.遍历元素 By KeyValuePair 这是一种关系,和java中的map.entry< , >类似 c# :
foreach (KeyValuePair<string, int> kvp in myDictionary) { Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value); }
java:
set<map.entry< , >> entrySet=map.entrySet();
Iterator<map,entry< , >> it=entryset.iterator();//利用迭代器便利关系map.entry< , >
while(it.hasnext())
{
map.entry< , > me= it.next();//这是一种关系
me.getkey();
me.getvalue();
}
5、仅遍历键 By Keys 属性 Dictionary<string, int>.KeyCollection keyCol = myDictionary.Keys; foreach (string key in keyCol/*string key in myDictionary.Keys*/) { Console.WriteLine("Key = {0}", key); } 6、仅遍历值By Valus属性 Dictionary<string, int>.ValueCollection valueCol = myDictionary.Values; foreach (int value in valueCol) { Console.WriteLine("Value = {0}", value); }