Dictionary的遍历和修改

简介: /// /// 初始化一个Dic /// public static void mainTest() { Dictionary dic = new Dictionary(); dic.

 

        /// <summary>
        /// 初始化一个Dic
        /// </summary>
        public static void mainTest()
        {
            Dictionary<String, Int32> dic = new Dictionary<String, Int32>();
            dic.Add("1", 1);
            dic.Add("2", 2);
            dic.Add("3", 3);

            Console.WriteLine("Org=============");
            foreachTravelDic(dic);

            modifyDic1(dic);
            Console.WriteLine("modify1=============");
            foreachTravelDic(dic);


            modifyDic2(dic);
            Console.WriteLine("modify2=============");
            foreachTravelDic(dic);

            Console.ReadKey();
        }

 

        /// <summary>
        /// foreach遍历Dictionary
        /// </summary>
        /// <param name="dic"></param>
        public static void foreachTravelDic(Dictionary<String, Int32> dic)
        {
            foreach (KeyValuePair<String, Int32> kvp in dic)
            {
                Console.WriteLine(String.Format("Key:{0}; Value:{1}", kvp.Key, kvp.Value));
                //dic[kvp.Key] = 2;//此操作会报错
            }
        }


        /// <summary>
        ///使用dic.Keys.ToArray方法遍历时更改Dic
        /// </summary>
        /// <param name="dic"></param>
        private static void modifyDic2(Dictionary<String, Int32> dic)
        {
            String[] keyStr = dic.Keys.ToArray<String>();
            for (int i = 0; i < keyStr.Length; i++)
            {
                dic[keyStr[i]] = dic[keyStr[i]] + 22;
            }
        }

        /// <summary>
        /// 使用dic.Keys.CopyTo方法遍历时更改Dic
        /// </summary>
        /// <param name="dic"></param>
        private static void modifyDic1(Dictionary<String, Int32> dic)
        {
            int dicCount = dic.Keys.Count;
            String[] strKey = new String[dicCount];
            dic.Keys.CopyTo(strKey, 0);//支持.net2.0
            for (int i = 0; i < strKey.Length; i++)
            {
                if (dic.ContainsKey(strKey[i]))
                {
                    dic[strKey[i]] = dic[strKey[i]] + 11;
                }
            }
        }

 

output:

Org=============
Key:1; Value:1
Key:2; Value:2
Key:3; Value:3
modify1=============
Key:1; Value:12
Key:2; Value:13
Key:3; Value:14
modify2=============
Key:1; Value:34
Key:2; Value:35
Key:3; Value:36

相关文章
|
Python
python遍历 truple list dictionary的几种方法
def TestDic1(): dict2 ={'aa':222,11:222} for val in dict2: print val def TestDic2(): dict2 ={'aa':222,11:222} for (key,val) in dict2.
866 0
(小常识)Dictionary的遍历
Dictionary objDictionary = new Dictionary();             objDictionary.Add(1, "张三");             objDictionary.
918 0
[AS3] 问个很囧的问题: 如何遍历Dictionary?
可以使用 for...in 循环或 for each...in 循环来遍历 Dictionary 对象的内容。   for...in 循环用于基于键进行遍历; 而 for each...in   循环用于基于与每个键关联的值进行遍历
遍历并修改dictionary的值
var dictionary = clb_Select.Items.Cast().ToDictionary(item => item.ToString(), item => false); foreach (var checkedItem in clb_Select.
744 0
.net 中Dictionary的遍历
            Dictionary dict = new Dictionary();             dict.Add(200, 1);             dict.Add(100, 2);             dict.
600 0
|
6月前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
78 3
|
6月前
|
存储 缓存 数据库连接
Python基础教程——字典(Dictionary)
Python基础教程——字典(Dictionary)
下一篇
无影云桌面